blob: 5552f8137571e640bfdcec380b9d3c3773a917df [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>
Hans Verkuil09965172010-08-01 14:32:42 -030023#include <media/v4l2-ioctl.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ctrls.h>
Hans Verkuil6e239392011-06-07 11:13:44 -030026#include <media/v4l2-event.h>
Hans Verkuil09965172010-08-01 14:32:42 -030027#include <media/v4l2-dev.h>
28
Hans Verkuilddac5c12011-06-10 05:43:34 -030029#define has_op(master, op) \
30 (master->ops && master->ops->op)
Hans Verkuil54c911e2011-05-25 06:04:58 -030031#define call_op(master, op) \
Hans Verkuilddac5c12011-06-10 05:43:34 -030032 (has_op(master, op) ? master->ops->op(master) : 0)
Hans Verkuil54c911e2011-05-25 06:04:58 -030033
Hans Verkuil09965172010-08-01 14:32:42 -030034/* Internal temporary helper struct, one for each v4l2_ext_control */
Hans Verkuileb5b16e2011-06-14 10:04:06 -030035struct v4l2_ctrl_helper {
36 /* Pointer to the control reference of the master control */
37 struct v4l2_ctrl_ref *mref;
Hans Verkuil09965172010-08-01 14:32:42 -030038 /* The control corresponding to the v4l2_ext_control ID field. */
39 struct v4l2_ctrl *ctrl;
Hans Verkuileb5b16e2011-06-14 10:04:06 -030040 /* v4l2_ext_control index of the next control belonging to the
41 same cluster, or 0 if there isn't any. */
42 u32 next;
Hans Verkuil09965172010-08-01 14:32:42 -030043};
44
Hans Verkuil72d877c2011-06-10 05:44:36 -030045/* Small helper function to determine if the autocluster is set to manual
Hans Verkuil88365102011-08-26 07:35:14 -030046 mode. */
Hans Verkuil72d877c2011-06-10 05:44:36 -030047static bool is_cur_manual(const struct v4l2_ctrl *master)
48{
49 return master->is_auto && master->cur.val == master->manual_mode_value;
50}
51
52/* Same as above, but this checks the against the new value instead of the
53 current value. */
54static bool is_new_manual(const struct v4l2_ctrl *master)
55{
56 return master->is_auto && master->val == master->manual_mode_value;
57}
58
Hans Verkuil09965172010-08-01 14:32:42 -030059/* Returns NULL or a character pointer array containing the menu for
60 the given control ID. The pointer array ends with a NULL pointer.
61 An empty string signifies a menu entry that is invalid. This allows
62 drivers to disable certain options if it is not supported. */
Hans Verkuil513521e2010-12-29 14:25:52 -030063const char * const *v4l2_ctrl_get_menu(u32 id)
Hans Verkuil09965172010-08-01 14:32:42 -030064{
Hans Verkuil513521e2010-12-29 14:25:52 -030065 static const char * const mpeg_audio_sampling_freq[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030066 "44.1 kHz",
67 "48 kHz",
68 "32 kHz",
69 NULL
70 };
Hans Verkuil513521e2010-12-29 14:25:52 -030071 static const char * const mpeg_audio_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030072 "MPEG-1/2 Layer I",
73 "MPEG-1/2 Layer II",
74 "MPEG-1/2 Layer III",
75 "MPEG-2/4 AAC",
76 "AC-3",
77 NULL
78 };
Hans Verkuil513521e2010-12-29 14:25:52 -030079 static const char * const mpeg_audio_l1_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030080 "32 kbps",
81 "64 kbps",
82 "96 kbps",
83 "128 kbps",
84 "160 kbps",
85 "192 kbps",
86 "224 kbps",
87 "256 kbps",
88 "288 kbps",
89 "320 kbps",
90 "352 kbps",
91 "384 kbps",
92 "416 kbps",
93 "448 kbps",
94 NULL
95 };
Hans Verkuil513521e2010-12-29 14:25:52 -030096 static const char * const mpeg_audio_l2_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -030097 "32 kbps",
98 "48 kbps",
99 "56 kbps",
100 "64 kbps",
101 "80 kbps",
102 "96 kbps",
103 "112 kbps",
104 "128 kbps",
105 "160 kbps",
106 "192 kbps",
107 "224 kbps",
108 "256 kbps",
109 "320 kbps",
110 "384 kbps",
111 NULL
112 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300113 static const char * const mpeg_audio_l3_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300114 "32 kbps",
115 "40 kbps",
116 "48 kbps",
117 "56 kbps",
118 "64 kbps",
119 "80 kbps",
120 "96 kbps",
121 "112 kbps",
122 "128 kbps",
123 "160 kbps",
124 "192 kbps",
125 "224 kbps",
126 "256 kbps",
127 "320 kbps",
128 NULL
129 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300130 static const char * const mpeg_audio_ac3_bitrate[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300131 "32 kbps",
132 "40 kbps",
133 "48 kbps",
134 "56 kbps",
135 "64 kbps",
136 "80 kbps",
137 "96 kbps",
138 "112 kbps",
139 "128 kbps",
140 "160 kbps",
141 "192 kbps",
142 "224 kbps",
143 "256 kbps",
144 "320 kbps",
145 "384 kbps",
146 "448 kbps",
147 "512 kbps",
148 "576 kbps",
149 "640 kbps",
150 NULL
151 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300152 static const char * const mpeg_audio_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300153 "Stereo",
154 "Joint Stereo",
155 "Dual",
156 "Mono",
157 NULL
158 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300159 static const char * const mpeg_audio_mode_extension[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300160 "Bound 4",
161 "Bound 8",
162 "Bound 12",
163 "Bound 16",
164 NULL
165 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300166 static const char * const mpeg_audio_emphasis[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300167 "No Emphasis",
168 "50/15 us",
169 "CCITT J17",
170 NULL
171 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300172 static const char * const mpeg_audio_crc[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300173 "No CRC",
174 "16-bit CRC",
175 NULL
176 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300177 static const char * const mpeg_video_encoding[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300178 "MPEG-1",
179 "MPEG-2",
180 "MPEG-4 AVC",
181 NULL
182 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300183 static const char * const mpeg_video_aspect[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300184 "1x1",
185 "4x3",
186 "16x9",
187 "2.21x1",
188 NULL
189 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300190 static const char * const mpeg_video_bitrate_mode[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300191 "Variable Bitrate",
192 "Constant Bitrate",
193 NULL
194 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300195 static const char * const mpeg_stream_type[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300196 "MPEG-2 Program Stream",
197 "MPEG-2 Transport Stream",
198 "MPEG-1 System Stream",
199 "MPEG-2 DVD-compatible Stream",
200 "MPEG-1 VCD-compatible Stream",
201 "MPEG-2 SVCD-compatible Stream",
202 NULL
203 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300204 static const char * const mpeg_stream_vbi_fmt[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300205 "No VBI",
Kamil Debski064f5092011-06-14 10:46:22 -0300206 "Private Packet, IVTV Format",
Hans Verkuil09965172010-08-01 14:32:42 -0300207 NULL
208 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300209 static const char * const camera_power_line_frequency[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300210 "Disabled",
211 "50 Hz",
212 "60 Hz",
Sylwester Nawrockid26a6632011-09-04 19:08:54 -0300213 "Auto",
Hans Verkuil09965172010-08-01 14:32:42 -0300214 NULL
215 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300216 static const char * const camera_exposure_auto[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300217 "Auto Mode",
218 "Manual Mode",
219 "Shutter Priority Mode",
220 "Aperture Priority Mode",
221 NULL
222 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300223 static const char * const colorfx[] = {
Hans Verkuil09965172010-08-01 14:32:42 -0300224 "None",
225 "Black & White",
226 "Sepia",
227 "Negative",
228 "Emboss",
229 "Sketch",
Kamil Debski064f5092011-06-14 10:46:22 -0300230 "Sky Blue",
231 "Grass Green",
232 "Skin Whiten",
Hans Verkuil09965172010-08-01 14:32:42 -0300233 "Vivid",
234 NULL
235 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300236 static const char * const tune_preemphasis[] = {
Kamil Debski064f5092011-06-14 10:46:22 -0300237 "No Preemphasis",
Hans Verkuil09965172010-08-01 14:32:42 -0300238 "50 useconds",
239 "75 useconds",
240 NULL,
241 };
Kamil Debski064f5092011-06-14 10:46:22 -0300242 static const char * const header_mode[] = {
243 "Separate Buffer",
244 "Joined With 1st Frame",
245 NULL,
246 };
247 static const char * const multi_slice[] = {
248 "Single",
249 "Max Macroblocks",
250 "Max Bytes",
251 NULL,
252 };
253 static const char * const entropy_mode[] = {
254 "CAVLC",
255 "CABAC",
256 NULL,
257 };
258 static const char * const mpeg_h264_level[] = {
259 "1",
260 "1b",
261 "1.1",
262 "1.2",
263 "1.3",
264 "2",
265 "2.1",
266 "2.2",
267 "3",
268 "3.1",
269 "3.2",
270 "4",
271 "4.1",
272 "4.2",
273 "5",
274 "5.1",
275 NULL,
276 };
277 static const char * const h264_loop_filter[] = {
278 "Enabled",
279 "Disabled",
280 "Disabled at Slice Boundary",
281 NULL,
282 };
283 static const char * const h264_profile[] = {
284 "Baseline",
285 "Constrained Baseline",
286 "Main",
287 "Extended",
288 "High",
289 "High 10",
290 "High 422",
291 "High 444 Predictive",
292 "High 10 Intra",
293 "High 422 Intra",
294 "High 444 Intra",
295 "CAVLC 444 Intra",
296 "Scalable Baseline",
297 "Scalable High",
298 "Scalable High Intra",
299 "Multiview High",
300 NULL,
301 };
302 static const char * const vui_sar_idc[] = {
303 "Unspecified",
304 "1:1",
305 "12:11",
306 "10:11",
307 "16:11",
308 "40:33",
309 "24:11",
310 "20:11",
311 "32:11",
312 "80:33",
313 "18:11",
314 "15:11",
315 "64:33",
316 "160:99",
317 "4:3",
318 "3:2",
319 "2:1",
320 "Extended SAR",
321 NULL,
322 };
323 static const char * const mpeg_mpeg4_level[] = {
324 "0",
325 "0b",
326 "1",
327 "2",
328 "3",
329 "3b",
330 "4",
331 "5",
332 NULL,
333 };
334 static const char * const mpeg4_profile[] = {
335 "Simple",
336 "Adcanved Simple",
337 "Core",
338 "Simple Scalable",
339 "Advanced Coding Efficency",
340 NULL,
341 };
342
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300343 static const char * const flash_led_mode[] = {
344 "Off",
345 "Flash",
346 "Torch",
347 NULL,
348 };
349 static const char * const flash_strobe_source[] = {
350 "Software",
351 "External",
352 NULL,
353 };
Hans Verkuil09965172010-08-01 14:32:42 -0300354
355 switch (id) {
356 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
357 return mpeg_audio_sampling_freq;
358 case V4L2_CID_MPEG_AUDIO_ENCODING:
359 return mpeg_audio_encoding;
360 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
361 return mpeg_audio_l1_bitrate;
362 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
363 return mpeg_audio_l2_bitrate;
364 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
365 return mpeg_audio_l3_bitrate;
366 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
367 return mpeg_audio_ac3_bitrate;
368 case V4L2_CID_MPEG_AUDIO_MODE:
369 return mpeg_audio_mode;
370 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
371 return mpeg_audio_mode_extension;
372 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
373 return mpeg_audio_emphasis;
374 case V4L2_CID_MPEG_AUDIO_CRC:
375 return mpeg_audio_crc;
376 case V4L2_CID_MPEG_VIDEO_ENCODING:
377 return mpeg_video_encoding;
378 case V4L2_CID_MPEG_VIDEO_ASPECT:
379 return mpeg_video_aspect;
380 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
381 return mpeg_video_bitrate_mode;
382 case V4L2_CID_MPEG_STREAM_TYPE:
383 return mpeg_stream_type;
384 case V4L2_CID_MPEG_STREAM_VBI_FMT:
385 return mpeg_stream_vbi_fmt;
386 case V4L2_CID_POWER_LINE_FREQUENCY:
387 return camera_power_line_frequency;
388 case V4L2_CID_EXPOSURE_AUTO:
389 return camera_exposure_auto;
390 case V4L2_CID_COLORFX:
391 return colorfx;
392 case V4L2_CID_TUNE_PREEMPHASIS:
393 return tune_preemphasis;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300394 case V4L2_CID_FLASH_LED_MODE:
395 return flash_led_mode;
396 case V4L2_CID_FLASH_STROBE_SOURCE:
397 return flash_strobe_source;
Kamil Debski064f5092011-06-14 10:46:22 -0300398 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
399 return header_mode;
400 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
401 return multi_slice;
402 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
403 return entropy_mode;
404 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
405 return mpeg_h264_level;
406 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
407 return h264_loop_filter;
408 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
409 return h264_profile;
410 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
411 return vui_sar_idc;
412 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
413 return mpeg_mpeg4_level;
414 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
415 return mpeg4_profile;
Hans Verkuil09965172010-08-01 14:32:42 -0300416 default:
417 return NULL;
418 }
419}
420EXPORT_SYMBOL(v4l2_ctrl_get_menu);
421
422/* Return the control name. */
423const char *v4l2_ctrl_get_name(u32 id)
424{
425 switch (id) {
426 /* USER controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300427 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300428 case V4L2_CID_USER_CLASS: return "User Controls";
429 case V4L2_CID_BRIGHTNESS: return "Brightness";
430 case V4L2_CID_CONTRAST: return "Contrast";
431 case V4L2_CID_SATURATION: return "Saturation";
432 case V4L2_CID_HUE: return "Hue";
433 case V4L2_CID_AUDIO_VOLUME: return "Volume";
434 case V4L2_CID_AUDIO_BALANCE: return "Balance";
435 case V4L2_CID_AUDIO_BASS: return "Bass";
436 case V4L2_CID_AUDIO_TREBLE: return "Treble";
437 case V4L2_CID_AUDIO_MUTE: return "Mute";
438 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Hans Verkuil09965172010-08-01 14:32:42 -0300439 case V4L2_CID_BLACK_LEVEL: return "Black Level";
440 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
441 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
442 case V4L2_CID_RED_BALANCE: return "Red Balance";
443 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
444 case V4L2_CID_GAMMA: return "Gamma";
445 case V4L2_CID_EXPOSURE: return "Exposure";
446 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
447 case V4L2_CID_GAIN: return "Gain";
448 case V4L2_CID_HFLIP: return "Horizontal Flip";
449 case V4L2_CID_VFLIP: return "Vertical Flip";
450 case V4L2_CID_HCENTER: return "Horizontal Center";
451 case V4L2_CID_VCENTER: return "Vertical Center";
452 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
453 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
454 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
455 case V4L2_CID_SHARPNESS: return "Sharpness";
456 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
457 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
Hans Verkuil09965172010-08-01 14:32:42 -0300458 case V4L2_CID_COLOR_KILLER: return "Color Killer";
459 case V4L2_CID_COLORFX: return "Color Effects";
460 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
461 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
462 case V4L2_CID_ROTATE: return "Rotate";
463 case V4L2_CID_BG_COLOR: return "Background Color";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300464 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
Jean-François Moine008d35f2010-09-13 07:04:49 -0300465 case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
466 case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
Kamil Debski064f5092011-06-14 10:46:22 -0300467 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: return "Minimum Number of Capture Buffers";
468 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Minimum Number of Output Buffers";
Hans Verkuil09965172010-08-01 14:32:42 -0300469
470 /* MPEG controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300471 /* Keep the order of the 'case's the same as in videodev2.h! */
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300472 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
473 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
474 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
475 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
476 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
477 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300478 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
479 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
480 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
Hans Verkuil09965172010-08-01 14:32:42 -0300481 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300482 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
483 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
484 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
485 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
486 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
Hans Verkuil09965172010-08-01 14:32:42 -0300487 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300488 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
489 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
490 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
491 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
492 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
493 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
494 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
495 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
496 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
497 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
498 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
499 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
500 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
501 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
Hans Verkuil09965172010-08-01 14:32:42 -0300502 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
Mauro Carvalho Chehab6dd5aff2010-08-06 09:57:02 -0300503 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
Hans Verkuil09965172010-08-01 14:32:42 -0300504 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
Kamil Debski064f5092011-06-14 10:46:22 -0300505 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE: return "Decoder Slice Interface";
506 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER: return "MPEG4 Loop Filter Enable";
507 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: return "The Number of Intra Refresh MBs";
508 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: return "Frame Level Rate Control Enable";
509 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: return "H264 MB Level Rate Control";
510 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: return "Sequence Header Mode";
511 case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC: return "The Max Number of Reference Picture";
512 case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP: return "H263 I-Frame QP Value";
513 case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP: return "H263 P frame QP Value";
514 case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP: return "H263 B frame QP Value";
515 case V4L2_CID_MPEG_VIDEO_H263_MIN_QP: return "H263 Minimum QP Value";
516 case V4L2_CID_MPEG_VIDEO_H263_MAX_QP: return "H263 Maximum QP Value";
517 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: return "H264 I-Frame QP Value";
518 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: return "H264 P frame QP Value";
519 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: return "H264 B frame QP Value";
520 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: return "H264 Maximum QP Value";
521 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: return "H264 Minimum QP Value";
522 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: return "H264 8x8 Transform Enable";
523 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: return "H264 CPB Buffer Size";
524 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: return "H264 Entorpy Mode";
525 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: return "H264 I Period";
526 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: return "H264 Level";
527 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: return "H264 Loop Filter Alpha Offset";
528 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: return "H264 Loop Filter Beta Offset";
529 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: return "H264 Loop Filter Mode";
530 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: return "H264 Profile";
531 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: return "Vertical Size of SAR";
532 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: return "Horizontal Size of SAR";
533 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: return "Aspect Ratio VUI Enable";
534 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: return "VUI Aspect Ratio IDC";
535 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: return "MPEG4 I-Frame QP Value";
536 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: return "MPEG4 P frame QP Value";
537 case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP: return "MPEG4 B frame QP Value";
538 case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP: return "MPEG4 Minimum QP Value";
539 case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP: return "MPEG4 Maximum QP Value";
540 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: return "MPEG4 Level";
541 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: return "MPEG4 Profile";
542 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL: return "Quarter Pixel Search Enable";
543 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: return "The Maximum Bytes Per Slice";
544 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: return "The Number of MB in a Slice";
545 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: return "The Slice Partitioning Method";
546 case V4L2_CID_MPEG_VIDEO_VBV_SIZE: return "VBV Buffer Size";
Hans Verkuil09965172010-08-01 14:32:42 -0300547
548 /* CAMERA controls */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300549 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300550 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
551 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
552 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
553 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
554 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
555 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
556 case V4L2_CID_PAN_RESET: return "Pan, Reset";
557 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
558 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
559 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
560 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
561 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
562 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
Hans Verkuil09965172010-08-01 14:32:42 -0300563 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
564 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
565 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
566 case V4L2_CID_PRIVACY: return "Privacy";
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300567 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
568 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
Hans Verkuil09965172010-08-01 14:32:42 -0300569
570 /* FM Radio Modulator control */
Hans Verkuil6c8d6112010-04-25 19:21:00 -0300571 /* Keep the order of the 'case's the same as in videodev2.h! */
Hans Verkuil09965172010-08-01 14:32:42 -0300572 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
573 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
574 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
575 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
576 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
577 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
578 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
579 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
580 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
581 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
582 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
583 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
584 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
585 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
586 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
587 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
588 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
589 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
590 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
591 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
592
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300593 /* Flash controls */
594 case V4L2_CID_FLASH_CLASS: return "Flash controls";
595 case V4L2_CID_FLASH_LED_MODE: return "LED mode";
596 case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe source";
597 case V4L2_CID_FLASH_STROBE: return "Strobe";
598 case V4L2_CID_FLASH_STROBE_STOP: return "Stop strobe";
599 case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe status";
600 case V4L2_CID_FLASH_TIMEOUT: return "Strobe timeout";
601 case V4L2_CID_FLASH_INTENSITY: return "Intensity, flash mode";
602 case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, torch mode";
603 case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, indicator";
604 case V4L2_CID_FLASH_FAULT: return "Faults";
605 case V4L2_CID_FLASH_CHARGE: return "Charge";
606 case V4L2_CID_FLASH_READY: return "Ready to strobe";
607
Hans Verkuil09965172010-08-01 14:32:42 -0300608 default:
609 return NULL;
610 }
611}
612EXPORT_SYMBOL(v4l2_ctrl_get_name);
613
614void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
615 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
616{
617 *name = v4l2_ctrl_get_name(id);
618 *flags = 0;
619
620 switch (id) {
621 case V4L2_CID_AUDIO_MUTE:
622 case V4L2_CID_AUDIO_LOUDNESS:
623 case V4L2_CID_AUTO_WHITE_BALANCE:
624 case V4L2_CID_AUTOGAIN:
625 case V4L2_CID_HFLIP:
626 case V4L2_CID_VFLIP:
627 case V4L2_CID_HUE_AUTO:
628 case V4L2_CID_CHROMA_AGC:
629 case V4L2_CID_COLOR_KILLER:
630 case V4L2_CID_MPEG_AUDIO_MUTE:
631 case V4L2_CID_MPEG_VIDEO_MUTE:
632 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
633 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
634 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
635 case V4L2_CID_FOCUS_AUTO:
636 case V4L2_CID_PRIVACY:
637 case V4L2_CID_AUDIO_LIMITER_ENABLED:
638 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
639 case V4L2_CID_PILOT_TONE_ENABLED:
Jean-François Moine008d35f2010-09-13 07:04:49 -0300640 case V4L2_CID_ILLUMINATORS_1:
641 case V4L2_CID_ILLUMINATORS_2:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300642 case V4L2_CID_FLASH_STROBE_STATUS:
643 case V4L2_CID_FLASH_CHARGE:
644 case V4L2_CID_FLASH_READY:
Kamil Debski064f5092011-06-14 10:46:22 -0300645 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
646 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
647 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
648 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
649 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
650 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
651 case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
Hans Verkuil09965172010-08-01 14:32:42 -0300652 *type = V4L2_CTRL_TYPE_BOOLEAN;
653 *min = 0;
654 *max = *step = 1;
655 break;
656 case V4L2_CID_PAN_RESET:
657 case V4L2_CID_TILT_RESET:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300658 case V4L2_CID_FLASH_STROBE:
659 case V4L2_CID_FLASH_STROBE_STOP:
Hans Verkuil09965172010-08-01 14:32:42 -0300660 *type = V4L2_CTRL_TYPE_BUTTON;
661 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
662 *min = *max = *step = *def = 0;
663 break;
664 case V4L2_CID_POWER_LINE_FREQUENCY:
665 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
666 case V4L2_CID_MPEG_AUDIO_ENCODING:
667 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
668 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
669 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
670 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
671 case V4L2_CID_MPEG_AUDIO_MODE:
672 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
673 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
674 case V4L2_CID_MPEG_AUDIO_CRC:
675 case V4L2_CID_MPEG_VIDEO_ENCODING:
676 case V4L2_CID_MPEG_VIDEO_ASPECT:
677 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
678 case V4L2_CID_MPEG_STREAM_TYPE:
679 case V4L2_CID_MPEG_STREAM_VBI_FMT:
680 case V4L2_CID_EXPOSURE_AUTO:
681 case V4L2_CID_COLORFX:
682 case V4L2_CID_TUNE_PREEMPHASIS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300683 case V4L2_CID_FLASH_LED_MODE:
684 case V4L2_CID_FLASH_STROBE_SOURCE:
Kamil Debski064f5092011-06-14 10:46:22 -0300685 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
686 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
687 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
688 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
689 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
690 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
691 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
692 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
693 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
Hans Verkuil09965172010-08-01 14:32:42 -0300694 *type = V4L2_CTRL_TYPE_MENU;
695 break;
696 case V4L2_CID_RDS_TX_PS_NAME:
697 case V4L2_CID_RDS_TX_RADIO_TEXT:
698 *type = V4L2_CTRL_TYPE_STRING;
699 break;
700 case V4L2_CID_USER_CLASS:
701 case V4L2_CID_CAMERA_CLASS:
702 case V4L2_CID_MPEG_CLASS:
703 case V4L2_CID_FM_TX_CLASS:
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300704 case V4L2_CID_FLASH_CLASS:
Hans Verkuil09965172010-08-01 14:32:42 -0300705 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
706 /* You can neither read not write these */
707 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
708 *min = *max = *step = *def = 0;
709 break;
710 case V4L2_CID_BG_COLOR:
711 *type = V4L2_CTRL_TYPE_INTEGER;
712 *step = 1;
713 *min = 0;
714 /* Max is calculated as RGB888 that is 2^24 */
715 *max = 0xFFFFFF;
716 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300717 case V4L2_CID_FLASH_FAULT:
718 *type = V4L2_CTRL_TYPE_BITMASK;
719 break;
Kamil Debski064f5092011-06-14 10:46:22 -0300720 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
721 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
722 *type = V4L2_CTRL_TYPE_INTEGER;
723 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
724 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300725 default:
726 *type = V4L2_CTRL_TYPE_INTEGER;
727 break;
728 }
729 switch (id) {
730 case V4L2_CID_MPEG_AUDIO_ENCODING:
731 case V4L2_CID_MPEG_AUDIO_MODE:
732 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
733 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
734 case V4L2_CID_MPEG_STREAM_TYPE:
735 *flags |= V4L2_CTRL_FLAG_UPDATE;
736 break;
737 case V4L2_CID_AUDIO_VOLUME:
738 case V4L2_CID_AUDIO_BALANCE:
739 case V4L2_CID_AUDIO_BASS:
740 case V4L2_CID_AUDIO_TREBLE:
741 case V4L2_CID_BRIGHTNESS:
742 case V4L2_CID_CONTRAST:
743 case V4L2_CID_SATURATION:
744 case V4L2_CID_HUE:
745 case V4L2_CID_RED_BALANCE:
746 case V4L2_CID_BLUE_BALANCE:
747 case V4L2_CID_GAMMA:
748 case V4L2_CID_SHARPNESS:
749 case V4L2_CID_CHROMA_GAIN:
750 case V4L2_CID_RDS_TX_DEVIATION:
751 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
752 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
753 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
754 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
755 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
756 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
757 case V4L2_CID_PILOT_TONE_DEVIATION:
758 case V4L2_CID_PILOT_TONE_FREQUENCY:
759 case V4L2_CID_TUNE_POWER_LEVEL:
760 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
761 *flags |= V4L2_CTRL_FLAG_SLIDER;
762 break;
763 case V4L2_CID_PAN_RELATIVE:
764 case V4L2_CID_TILT_RELATIVE:
765 case V4L2_CID_FOCUS_RELATIVE:
766 case V4L2_CID_IRIS_RELATIVE:
767 case V4L2_CID_ZOOM_RELATIVE:
768 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
769 break;
Sakari Ailus0b159ac2011-03-21 12:52:51 -0300770 case V4L2_CID_FLASH_STROBE_STATUS:
771 case V4L2_CID_FLASH_READY:
772 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
773 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300774 }
775}
776EXPORT_SYMBOL(v4l2_ctrl_fill);
777
778/* Helper function to determine whether the control type is compatible with
779 VIDIOC_G/S_CTRL. */
780static bool type_is_int(const struct v4l2_ctrl *ctrl)
781{
782 switch (ctrl->type) {
783 case V4L2_CTRL_TYPE_INTEGER64:
784 case V4L2_CTRL_TYPE_STRING:
785 /* Nope, these need v4l2_ext_control */
786 return false;
787 default:
788 return true;
789 }
790}
791
Hans Verkuil6e239392011-06-07 11:13:44 -0300792static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
793{
794 memset(ev->reserved, 0, sizeof(ev->reserved));
795 ev->type = V4L2_EVENT_CTRL;
796 ev->id = ctrl->id;
797 ev->u.ctrl.changes = changes;
798 ev->u.ctrl.type = ctrl->type;
799 ev->u.ctrl.flags = ctrl->flags;
800 if (ctrl->type == V4L2_CTRL_TYPE_STRING)
801 ev->u.ctrl.value64 = 0;
802 else
803 ev->u.ctrl.value64 = ctrl->cur.val64;
804 ev->u.ctrl.minimum = ctrl->minimum;
805 ev->u.ctrl.maximum = ctrl->maximum;
806 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
807 ev->u.ctrl.step = 1;
808 else
809 ev->u.ctrl.step = ctrl->step;
810 ev->u.ctrl.default_value = ctrl->default_value;
811}
812
813static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
814{
815 struct v4l2_event ev;
Hans Verkuil77068d32011-06-13 18:55:58 -0300816 struct v4l2_subscribed_event *sev;
Hans Verkuil6e239392011-06-07 11:13:44 -0300817
Hans Verkuil77068d32011-06-13 18:55:58 -0300818 if (list_empty(&ctrl->ev_subs))
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300819 return;
Hans Verkuil6e239392011-06-07 11:13:44 -0300820 fill_event(&ev, ctrl, changes);
821
Hans Verkuil77068d32011-06-13 18:55:58 -0300822 list_for_each_entry(sev, &ctrl->ev_subs, node)
Hans Verkuil60c073222011-06-29 08:56:22 -0300823 if (sev->fh && (sev->fh != fh ||
824 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)))
Hans Verkuil77068d32011-06-13 18:55:58 -0300825 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300826}
827
Hans Verkuil09965172010-08-01 14:32:42 -0300828/* Helper function: copy the current control value back to the caller */
829static int cur_to_user(struct v4l2_ext_control *c,
830 struct v4l2_ctrl *ctrl)
831{
832 u32 len;
833
834 switch (ctrl->type) {
835 case V4L2_CTRL_TYPE_STRING:
836 len = strlen(ctrl->cur.string);
837 if (c->size < len + 1) {
838 c->size = len + 1;
839 return -ENOSPC;
840 }
841 return copy_to_user(c->string, ctrl->cur.string,
842 len + 1) ? -EFAULT : 0;
843 case V4L2_CTRL_TYPE_INTEGER64:
844 c->value64 = ctrl->cur.val64;
845 break;
846 default:
847 c->value = ctrl->cur.val;
848 break;
849 }
850 return 0;
851}
852
853/* Helper function: copy the caller-provider value as the new control value */
854static int user_to_new(struct v4l2_ext_control *c,
855 struct v4l2_ctrl *ctrl)
856{
857 int ret;
858 u32 size;
859
Hans Verkuil2a863792011-01-11 14:45:03 -0300860 ctrl->is_new = 1;
Hans Verkuil09965172010-08-01 14:32:42 -0300861 switch (ctrl->type) {
862 case V4L2_CTRL_TYPE_INTEGER64:
863 ctrl->val64 = c->value64;
864 break;
865 case V4L2_CTRL_TYPE_STRING:
866 size = c->size;
867 if (size == 0)
868 return -ERANGE;
869 if (size > ctrl->maximum + 1)
870 size = ctrl->maximum + 1;
871 ret = copy_from_user(ctrl->string, c->string, size);
872 if (!ret) {
873 char last = ctrl->string[size - 1];
874
875 ctrl->string[size - 1] = 0;
876 /* If the string was longer than ctrl->maximum,
877 then return an error. */
878 if (strlen(ctrl->string) == ctrl->maximum && last)
879 return -ERANGE;
880 }
881 return ret ? -EFAULT : 0;
882 default:
883 ctrl->val = c->value;
884 break;
885 }
886 return 0;
887}
888
889/* Helper function: copy the new control value back to the caller */
890static int new_to_user(struct v4l2_ext_control *c,
891 struct v4l2_ctrl *ctrl)
892{
893 u32 len;
894
895 switch (ctrl->type) {
896 case V4L2_CTRL_TYPE_STRING:
897 len = strlen(ctrl->string);
898 if (c->size < len + 1) {
899 c->size = ctrl->maximum + 1;
900 return -ENOSPC;
901 }
902 return copy_to_user(c->string, ctrl->string,
903 len + 1) ? -EFAULT : 0;
904 case V4L2_CTRL_TYPE_INTEGER64:
905 c->value64 = ctrl->val64;
906 break;
907 default:
908 c->value = ctrl->val;
909 break;
910 }
911 return 0;
912}
913
914/* Copy the new value to the current value. */
Hans Verkuilab892ba2011-06-07 06:47:18 -0300915static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
916 bool update_inactive)
Hans Verkuil09965172010-08-01 14:32:42 -0300917{
Hans Verkuil6e239392011-06-07 11:13:44 -0300918 bool changed = false;
919
Hans Verkuil09965172010-08-01 14:32:42 -0300920 if (ctrl == NULL)
921 return;
922 switch (ctrl->type) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300923 case V4L2_CTRL_TYPE_BUTTON:
924 changed = true;
925 break;
Hans Verkuil09965172010-08-01 14:32:42 -0300926 case V4L2_CTRL_TYPE_STRING:
927 /* strings are always 0-terminated */
Hans Verkuil6e239392011-06-07 11:13:44 -0300928 changed = strcmp(ctrl->string, ctrl->cur.string);
Hans Verkuil09965172010-08-01 14:32:42 -0300929 strcpy(ctrl->cur.string, ctrl->string);
930 break;
931 case V4L2_CTRL_TYPE_INTEGER64:
Hans Verkuil6e239392011-06-07 11:13:44 -0300932 changed = ctrl->val64 != ctrl->cur.val64;
Hans Verkuil09965172010-08-01 14:32:42 -0300933 ctrl->cur.val64 = ctrl->val64;
934 break;
935 default:
Hans Verkuil6e239392011-06-07 11:13:44 -0300936 changed = ctrl->val != ctrl->cur.val;
Hans Verkuil09965172010-08-01 14:32:42 -0300937 ctrl->cur.val = ctrl->val;
938 break;
939 }
Hans Verkuil72d877c2011-06-10 05:44:36 -0300940 if (update_inactive) {
Hans Verkuil5626b8c2011-08-26 07:53:53 -0300941 /* Note: update_inactive can only be true for auto clusters. */
942 ctrl->flags &=
943 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
944 if (!is_cur_manual(ctrl->cluster[0])) {
Hans Verkuil72d877c2011-06-10 05:44:36 -0300945 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
Hans Verkuil5626b8c2011-08-26 07:53:53 -0300946 if (ctrl->cluster[0]->has_volatiles)
947 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
948 }
Hans Verkuil72d877c2011-06-10 05:44:36 -0300949 }
Hans Verkuil639884a2011-07-05 07:09:26 -0300950 if (changed || update_inactive) {
951 /* If a control was changed that was not one of the controls
952 modified by the application, then send the event to all. */
953 if (!ctrl->is_new)
954 fh = NULL;
Hans Verkuil6e239392011-06-07 11:13:44 -0300955 send_event(fh, ctrl,
956 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
957 (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
Hans Verkuil639884a2011-07-05 07:09:26 -0300958 }
Hans Verkuil09965172010-08-01 14:32:42 -0300959}
960
961/* Copy the current value to the new value */
962static void cur_to_new(struct v4l2_ctrl *ctrl)
963{
964 if (ctrl == NULL)
965 return;
966 switch (ctrl->type) {
967 case V4L2_CTRL_TYPE_STRING:
968 /* strings are always 0-terminated */
969 strcpy(ctrl->string, ctrl->cur.string);
970 break;
971 case V4L2_CTRL_TYPE_INTEGER64:
972 ctrl->val64 = ctrl->cur.val64;
973 break;
974 default:
975 ctrl->val = ctrl->cur.val;
976 break;
977 }
978}
979
980/* Return non-zero if one or more of the controls in the cluster has a new
981 value that differs from the current value. */
982static int cluster_changed(struct v4l2_ctrl *master)
983{
984 int diff = 0;
985 int i;
986
987 for (i = 0; !diff && i < master->ncontrols; i++) {
988 struct v4l2_ctrl *ctrl = master->cluster[i];
989
990 if (ctrl == NULL)
991 continue;
992 switch (ctrl->type) {
993 case V4L2_CTRL_TYPE_BUTTON:
994 /* Button controls are always 'different' */
995 return 1;
996 case V4L2_CTRL_TYPE_STRING:
997 /* strings are always 0-terminated */
998 diff = strcmp(ctrl->string, ctrl->cur.string);
999 break;
1000 case V4L2_CTRL_TYPE_INTEGER64:
1001 diff = ctrl->val64 != ctrl->cur.val64;
1002 break;
1003 default:
1004 diff = ctrl->val != ctrl->cur.val;
1005 break;
1006 }
1007 }
1008 return diff;
1009}
1010
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001011/* Validate integer-type control */
1012static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
Hans Verkuil09965172010-08-01 14:32:42 -03001013{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001014 s32 val = *pval;
Hans Verkuil09965172010-08-01 14:32:42 -03001015 u32 offset;
Hans Verkuil09965172010-08-01 14:32:42 -03001016
1017 switch (ctrl->type) {
1018 case V4L2_CTRL_TYPE_INTEGER:
1019 /* Round towards the closest legal value */
1020 val += ctrl->step / 2;
1021 if (val < ctrl->minimum)
1022 val = ctrl->minimum;
1023 if (val > ctrl->maximum)
1024 val = ctrl->maximum;
1025 offset = val - ctrl->minimum;
1026 offset = ctrl->step * (offset / ctrl->step);
1027 val = ctrl->minimum + offset;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001028 *pval = val;
Hans Verkuil09965172010-08-01 14:32:42 -03001029 return 0;
1030
1031 case V4L2_CTRL_TYPE_BOOLEAN:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001032 *pval = !!val;
Hans Verkuil09965172010-08-01 14:32:42 -03001033 return 0;
1034
1035 case V4L2_CTRL_TYPE_MENU:
1036 if (val < ctrl->minimum || val > ctrl->maximum)
1037 return -ERANGE;
1038 if (ctrl->qmenu[val][0] == '\0' ||
1039 (ctrl->menu_skip_mask & (1 << val)))
1040 return -EINVAL;
1041 return 0;
1042
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001043 case V4L2_CTRL_TYPE_BITMASK:
1044 *pval &= ctrl->maximum;
1045 return 0;
1046
Hans Verkuil09965172010-08-01 14:32:42 -03001047 case V4L2_CTRL_TYPE_BUTTON:
1048 case V4L2_CTRL_TYPE_CTRL_CLASS:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001049 *pval = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001050 return 0;
1051
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001052 default:
1053 return -EINVAL;
1054 }
1055}
1056
1057/* Validate a new control */
1058static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
1059{
1060 char *s = c->string;
1061 size_t len;
1062
1063 switch (ctrl->type) {
1064 case V4L2_CTRL_TYPE_INTEGER:
1065 case V4L2_CTRL_TYPE_BOOLEAN:
1066 case V4L2_CTRL_TYPE_MENU:
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001067 case V4L2_CTRL_TYPE_BITMASK:
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001068 case V4L2_CTRL_TYPE_BUTTON:
1069 case V4L2_CTRL_TYPE_CTRL_CLASS:
1070 return validate_new_int(ctrl, &c->value);
1071
Hans Verkuil09965172010-08-01 14:32:42 -03001072 case V4L2_CTRL_TYPE_INTEGER64:
1073 return 0;
1074
1075 case V4L2_CTRL_TYPE_STRING:
1076 len = strlen(s);
1077 if (len < ctrl->minimum)
1078 return -ERANGE;
1079 if ((len - ctrl->minimum) % ctrl->step)
1080 return -ERANGE;
1081 return 0;
1082
1083 default:
1084 return -EINVAL;
1085 }
1086}
1087
1088static inline u32 node2id(struct list_head *node)
1089{
1090 return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
1091}
1092
1093/* Set the handler's error code if it wasn't set earlier already */
1094static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1095{
1096 if (hdl->error == 0)
1097 hdl->error = err;
1098 return err;
1099}
1100
1101/* Initialize the handler */
1102int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
1103 unsigned nr_of_controls_hint)
1104{
1105 mutex_init(&hdl->lock);
1106 INIT_LIST_HEAD(&hdl->ctrls);
1107 INIT_LIST_HEAD(&hdl->ctrl_refs);
1108 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1109 hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
1110 GFP_KERNEL);
1111 hdl->error = hdl->buckets ? 0 : -ENOMEM;
1112 return hdl->error;
1113}
1114EXPORT_SYMBOL(v4l2_ctrl_handler_init);
1115
1116/* Free all controls and control refs */
1117void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1118{
1119 struct v4l2_ctrl_ref *ref, *next_ref;
1120 struct v4l2_ctrl *ctrl, *next_ctrl;
Hans Verkuil77068d32011-06-13 18:55:58 -03001121 struct v4l2_subscribed_event *sev, *next_sev;
Hans Verkuil09965172010-08-01 14:32:42 -03001122
1123 if (hdl == NULL || hdl->buckets == NULL)
1124 return;
1125
1126 mutex_lock(&hdl->lock);
1127 /* Free all nodes */
1128 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1129 list_del(&ref->node);
1130 kfree(ref);
1131 }
1132 /* Free all controls owned by the handler */
1133 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1134 list_del(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001135 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1136 list_del(&sev->node);
Hans Verkuil09965172010-08-01 14:32:42 -03001137 kfree(ctrl);
1138 }
1139 kfree(hdl->buckets);
1140 hdl->buckets = NULL;
1141 hdl->cached = NULL;
1142 hdl->error = 0;
1143 mutex_unlock(&hdl->lock);
1144}
1145EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1146
1147/* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1148 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1149 with applications that do not use the NEXT_CTRL flag.
1150
1151 We just find the n-th private user control. It's O(N), but that should not
1152 be an issue in this particular case. */
1153static struct v4l2_ctrl_ref *find_private_ref(
1154 struct v4l2_ctrl_handler *hdl, u32 id)
1155{
1156 struct v4l2_ctrl_ref *ref;
1157
1158 id -= V4L2_CID_PRIVATE_BASE;
1159 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1160 /* Search for private user controls that are compatible with
1161 VIDIOC_G/S_CTRL. */
1162 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1163 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1164 if (!type_is_int(ref->ctrl))
1165 continue;
1166 if (id == 0)
1167 return ref;
1168 id--;
1169 }
1170 }
1171 return NULL;
1172}
1173
1174/* Find a control with the given ID. */
1175static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1176{
1177 struct v4l2_ctrl_ref *ref;
1178 int bucket;
1179
1180 id &= V4L2_CTRL_ID_MASK;
1181
1182 /* Old-style private controls need special handling */
1183 if (id >= V4L2_CID_PRIVATE_BASE)
1184 return find_private_ref(hdl, id);
1185 bucket = id % hdl->nr_of_buckets;
1186
1187 /* Simple optimization: cache the last control found */
1188 if (hdl->cached && hdl->cached->ctrl->id == id)
1189 return hdl->cached;
1190
1191 /* Not in cache, search the hash */
1192 ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1193 while (ref && ref->ctrl->id != id)
1194 ref = ref->next;
1195
1196 if (ref)
1197 hdl->cached = ref; /* cache it! */
1198 return ref;
1199}
1200
1201/* Find a control with the given ID. Take the handler's lock first. */
1202static struct v4l2_ctrl_ref *find_ref_lock(
1203 struct v4l2_ctrl_handler *hdl, u32 id)
1204{
1205 struct v4l2_ctrl_ref *ref = NULL;
1206
1207 if (hdl) {
1208 mutex_lock(&hdl->lock);
1209 ref = find_ref(hdl, id);
1210 mutex_unlock(&hdl->lock);
1211 }
1212 return ref;
1213}
1214
1215/* Find a control with the given ID. */
1216struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1217{
1218 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1219
1220 return ref ? ref->ctrl : NULL;
1221}
1222EXPORT_SYMBOL(v4l2_ctrl_find);
1223
1224/* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1225static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1226 struct v4l2_ctrl *ctrl)
1227{
1228 struct v4l2_ctrl_ref *ref;
1229 struct v4l2_ctrl_ref *new_ref;
1230 u32 id = ctrl->id;
1231 u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
1232 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
1233
1234 /* Automatically add the control class if it is not yet present. */
1235 if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1236 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1237 return hdl->error;
1238
1239 if (hdl->error)
1240 return hdl->error;
1241
1242 new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1243 if (!new_ref)
1244 return handler_set_err(hdl, -ENOMEM);
1245 new_ref->ctrl = ctrl;
1246 if (ctrl->handler == hdl) {
1247 /* By default each control starts in a cluster of its own.
1248 new_ref->ctrl is basically a cluster array with one
1249 element, so that's perfect to use as the cluster pointer.
1250 But only do this for the handler that owns the control. */
1251 ctrl->cluster = &new_ref->ctrl;
1252 ctrl->ncontrols = 1;
1253 }
1254
1255 INIT_LIST_HEAD(&new_ref->node);
1256
1257 mutex_lock(&hdl->lock);
1258
1259 /* Add immediately at the end of the list if the list is empty, or if
1260 the last element in the list has a lower ID.
1261 This ensures that when elements are added in ascending order the
1262 insertion is an O(1) operation. */
1263 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1264 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1265 goto insert_in_hash;
1266 }
1267
1268 /* Find insert position in sorted list */
1269 list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1270 if (ref->ctrl->id < id)
1271 continue;
1272 /* Don't add duplicates */
1273 if (ref->ctrl->id == id) {
1274 kfree(new_ref);
1275 goto unlock;
1276 }
1277 list_add(&new_ref->node, ref->node.prev);
1278 break;
1279 }
1280
1281insert_in_hash:
1282 /* Insert the control node in the hash */
1283 new_ref->next = hdl->buckets[bucket];
1284 hdl->buckets[bucket] = new_ref;
1285
1286unlock:
1287 mutex_unlock(&hdl->lock);
1288 return 0;
1289}
1290
1291/* Add a new control */
1292static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1293 const struct v4l2_ctrl_ops *ops,
1294 u32 id, const char *name, enum v4l2_ctrl_type type,
1295 s32 min, s32 max, u32 step, s32 def,
Hans Verkuil513521e2010-12-29 14:25:52 -03001296 u32 flags, const char * const *qmenu, void *priv)
Hans Verkuil09965172010-08-01 14:32:42 -03001297{
1298 struct v4l2_ctrl *ctrl;
1299 unsigned sz_extra = 0;
1300
1301 if (hdl->error)
1302 return NULL;
1303
1304 /* Sanity checks */
1305 if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
Hans Verkuil09965172010-08-01 14:32:42 -03001306 (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001307 (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
Hans Verkuil09965172010-08-01 14:32:42 -03001308 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1309 (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1310 handler_set_err(hdl, -ERANGE);
1311 return NULL;
1312 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001313 if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1314 handler_set_err(hdl, -ERANGE);
1315 return NULL;
1316 }
Hans Verkuil02ac0482010-12-29 14:27:05 -03001317 if ((type == V4L2_CTRL_TYPE_INTEGER ||
1318 type == V4L2_CTRL_TYPE_MENU ||
1319 type == V4L2_CTRL_TYPE_BOOLEAN) &&
1320 (def < min || def > max)) {
1321 handler_set_err(hdl, -ERANGE);
1322 return NULL;
1323 }
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001324 if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1325 handler_set_err(hdl, -ERANGE);
1326 return NULL;
1327 }
Hans Verkuil09965172010-08-01 14:32:42 -03001328
1329 if (type == V4L2_CTRL_TYPE_BUTTON)
1330 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1331 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1332 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1333 else if (type == V4L2_CTRL_TYPE_STRING)
1334 sz_extra += 2 * (max + 1);
1335
1336 ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1337 if (ctrl == NULL) {
1338 handler_set_err(hdl, -ENOMEM);
1339 return NULL;
1340 }
1341
1342 INIT_LIST_HEAD(&ctrl->node);
Hans Verkuil77068d32011-06-13 18:55:58 -03001343 INIT_LIST_HEAD(&ctrl->ev_subs);
Hans Verkuil09965172010-08-01 14:32:42 -03001344 ctrl->handler = hdl;
1345 ctrl->ops = ops;
1346 ctrl->id = id;
1347 ctrl->name = name;
1348 ctrl->type = type;
1349 ctrl->flags = flags;
1350 ctrl->minimum = min;
1351 ctrl->maximum = max;
1352 ctrl->step = step;
1353 ctrl->qmenu = qmenu;
1354 ctrl->priv = priv;
1355 ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1356
1357 if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1358 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1359 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1360 if (ctrl->minimum)
1361 memset(ctrl->cur.string, ' ', ctrl->minimum);
1362 }
1363 if (handler_new_ref(hdl, ctrl)) {
1364 kfree(ctrl);
1365 return NULL;
1366 }
1367 mutex_lock(&hdl->lock);
1368 list_add_tail(&ctrl->node, &hdl->ctrls);
1369 mutex_unlock(&hdl->lock);
1370 return ctrl;
1371}
1372
1373struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1374 const struct v4l2_ctrl_config *cfg, void *priv)
1375{
1376 bool is_menu;
1377 struct v4l2_ctrl *ctrl;
1378 const char *name = cfg->name;
Hans Verkuil513521e2010-12-29 14:25:52 -03001379 const char * const *qmenu = cfg->qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -03001380 enum v4l2_ctrl_type type = cfg->type;
1381 u32 flags = cfg->flags;
1382 s32 min = cfg->min;
1383 s32 max = cfg->max;
1384 u32 step = cfg->step;
1385 s32 def = cfg->def;
1386
1387 if (name == NULL)
1388 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1389 &def, &flags);
1390
1391 is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1392 if (is_menu)
1393 WARN_ON(step);
1394 else
1395 WARN_ON(cfg->menu_skip_mask);
1396 if (is_menu && qmenu == NULL)
1397 qmenu = v4l2_ctrl_get_menu(cfg->id);
1398
1399 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1400 type, min, max,
1401 is_menu ? cfg->menu_skip_mask : step,
1402 def, flags, qmenu, priv);
Hans Verkuil88365102011-08-26 07:35:14 -03001403 if (ctrl)
Hans Verkuil09965172010-08-01 14:32:42 -03001404 ctrl->is_private = cfg->is_private;
Hans Verkuil09965172010-08-01 14:32:42 -03001405 return ctrl;
1406}
1407EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1408
1409/* Helper function for standard non-menu controls */
1410struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1411 const struct v4l2_ctrl_ops *ops,
1412 u32 id, s32 min, s32 max, u32 step, s32 def)
1413{
1414 const char *name;
1415 enum v4l2_ctrl_type type;
1416 u32 flags;
1417
1418 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1419 if (type == V4L2_CTRL_TYPE_MENU) {
1420 handler_set_err(hdl, -EINVAL);
1421 return NULL;
1422 }
1423 return v4l2_ctrl_new(hdl, ops, id, name, type,
1424 min, max, step, def, flags, NULL, NULL);
1425}
1426EXPORT_SYMBOL(v4l2_ctrl_new_std);
1427
1428/* Helper function for standard menu controls */
1429struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1430 const struct v4l2_ctrl_ops *ops,
1431 u32 id, s32 max, s32 mask, s32 def)
1432{
Hans Verkuil513521e2010-12-29 14:25:52 -03001433 const char * const *qmenu = v4l2_ctrl_get_menu(id);
Hans Verkuil09965172010-08-01 14:32:42 -03001434 const char *name;
1435 enum v4l2_ctrl_type type;
1436 s32 min;
1437 s32 step;
1438 u32 flags;
1439
1440 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1441 if (type != V4L2_CTRL_TYPE_MENU) {
1442 handler_set_err(hdl, -EINVAL);
1443 return NULL;
1444 }
1445 return v4l2_ctrl_new(hdl, ops, id, name, type,
1446 0, max, mask, def, flags, qmenu, NULL);
1447}
1448EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1449
1450/* Add a control from another handler to this handler */
1451struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1452 struct v4l2_ctrl *ctrl)
1453{
1454 if (hdl == NULL || hdl->error)
1455 return NULL;
1456 if (ctrl == NULL) {
1457 handler_set_err(hdl, -EINVAL);
1458 return NULL;
1459 }
1460 if (ctrl->handler == hdl)
1461 return ctrl;
1462 return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1463}
1464EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1465
1466/* Add the controls from another handler to our own. */
1467int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1468 struct v4l2_ctrl_handler *add)
1469{
1470 struct v4l2_ctrl *ctrl;
1471 int ret = 0;
1472
1473 /* Do nothing if either handler is NULL or if they are the same */
1474 if (!hdl || !add || hdl == add)
1475 return 0;
1476 if (hdl->error)
1477 return hdl->error;
1478 mutex_lock(&add->lock);
1479 list_for_each_entry(ctrl, &add->ctrls, node) {
1480 /* Skip handler-private controls. */
1481 if (ctrl->is_private)
1482 continue;
Hans Verkuil6e239392011-06-07 11:13:44 -03001483 /* And control classes */
1484 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1485 continue;
Hans Verkuil09965172010-08-01 14:32:42 -03001486 ret = handler_new_ref(hdl, ctrl);
1487 if (ret)
1488 break;
1489 }
1490 mutex_unlock(&add->lock);
1491 return ret;
1492}
1493EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1494
1495/* Cluster controls */
1496void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1497{
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001498 bool has_volatiles = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001499 int i;
1500
1501 /* The first control is the master control and it must not be NULL */
Hans Verkuil72d877c2011-06-10 05:44:36 -03001502 BUG_ON(ncontrols == 0 || controls[0] == NULL);
Hans Verkuil09965172010-08-01 14:32:42 -03001503
1504 for (i = 0; i < ncontrols; i++) {
1505 if (controls[i]) {
1506 controls[i]->cluster = controls;
1507 controls[i]->ncontrols = ncontrols;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001508 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
1509 has_volatiles = true;
Hans Verkuil09965172010-08-01 14:32:42 -03001510 }
1511 }
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001512 controls[0]->has_volatiles = has_volatiles;
Hans Verkuil09965172010-08-01 14:32:42 -03001513}
1514EXPORT_SYMBOL(v4l2_ctrl_cluster);
1515
Hans Verkuil72d877c2011-06-10 05:44:36 -03001516void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1517 u8 manual_val, bool set_volatile)
1518{
1519 struct v4l2_ctrl *master = controls[0];
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001520 u32 flag = 0;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001521 int i;
1522
1523 v4l2_ctrl_cluster(ncontrols, controls);
1524 WARN_ON(ncontrols <= 1);
Hans Verkuil82a7c042011-06-28 10:43:13 -03001525 WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001526 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
Hans Verkuil72d877c2011-06-10 05:44:36 -03001527 master->is_auto = true;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001528 master->has_volatiles = set_volatile;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001529 master->manual_mode_value = manual_val;
1530 master->flags |= V4L2_CTRL_FLAG_UPDATE;
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001531
1532 if (!is_cur_manual(master))
1533 flag = V4L2_CTRL_FLAG_INACTIVE |
1534 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03001535
1536 for (i = 1; i < ncontrols; i++)
Hans Verkuil88365102011-08-26 07:35:14 -03001537 if (controls[i])
Hans Verkuil72d877c2011-06-10 05:44:36 -03001538 controls[i]->flags |= flag;
Hans Verkuil72d877c2011-06-10 05:44:36 -03001539}
1540EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1541
Hans Verkuil09965172010-08-01 14:32:42 -03001542/* Activate/deactivate a control. */
1543void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1544{
Hans Verkuil6e239392011-06-07 11:13:44 -03001545 /* invert since the actual flag is called 'inactive' */
1546 bool inactive = !active;
1547 bool old;
1548
Hans Verkuil09965172010-08-01 14:32:42 -03001549 if (ctrl == NULL)
1550 return;
1551
Hans Verkuil6e239392011-06-07 11:13:44 -03001552 if (inactive)
Hans Verkuil09965172010-08-01 14:32:42 -03001553 /* set V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001554 old = test_and_set_bit(4, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001555 else
1556 /* clear V4L2_CTRL_FLAG_INACTIVE */
Hans Verkuil6e239392011-06-07 11:13:44 -03001557 old = test_and_clear_bit(4, &ctrl->flags);
1558 if (old != inactive)
1559 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
Hans Verkuil09965172010-08-01 14:32:42 -03001560}
1561EXPORT_SYMBOL(v4l2_ctrl_activate);
1562
1563/* Grab/ungrab a control.
1564 Typically used when streaming starts and you want to grab controls,
1565 preventing the user from changing them.
1566
1567 Just call this and the framework will block any attempts to change
1568 these controls. */
1569void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1570{
Hans Verkuil6e239392011-06-07 11:13:44 -03001571 bool old;
1572
Hans Verkuil09965172010-08-01 14:32:42 -03001573 if (ctrl == NULL)
1574 return;
1575
Hans Verkuil6e239392011-06-07 11:13:44 -03001576 v4l2_ctrl_lock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001577 if (grabbed)
1578 /* set V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001579 old = test_and_set_bit(1, &ctrl->flags);
Hans Verkuil09965172010-08-01 14:32:42 -03001580 else
1581 /* clear V4L2_CTRL_FLAG_GRABBED */
Hans Verkuil6e239392011-06-07 11:13:44 -03001582 old = test_and_clear_bit(1, &ctrl->flags);
1583 if (old != grabbed)
1584 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1585 v4l2_ctrl_unlock(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001586}
1587EXPORT_SYMBOL(v4l2_ctrl_grab);
1588
1589/* Log the control name and value */
1590static void log_ctrl(const struct v4l2_ctrl *ctrl,
1591 const char *prefix, const char *colon)
1592{
Hans Verkuil09965172010-08-01 14:32:42 -03001593 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1594 return;
1595 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1596 return;
1597
1598 printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1599
1600 switch (ctrl->type) {
1601 case V4L2_CTRL_TYPE_INTEGER:
1602 printk(KERN_CONT "%d", ctrl->cur.val);
1603 break;
1604 case V4L2_CTRL_TYPE_BOOLEAN:
1605 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1606 break;
1607 case V4L2_CTRL_TYPE_MENU:
1608 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1609 break;
Hans Verkuilfa4d7092011-05-23 04:07:05 -03001610 case V4L2_CTRL_TYPE_BITMASK:
1611 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1612 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001613 case V4L2_CTRL_TYPE_INTEGER64:
1614 printk(KERN_CONT "%lld", ctrl->cur.val64);
1615 break;
1616 case V4L2_CTRL_TYPE_STRING:
1617 printk(KERN_CONT "%s", ctrl->cur.string);
1618 break;
1619 default:
1620 printk(KERN_CONT "unknown type %d", ctrl->type);
1621 break;
1622 }
Hans Verkuil88365102011-08-26 07:35:14 -03001623 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
1624 V4L2_CTRL_FLAG_GRABBED |
1625 V4L2_CTRL_FLAG_VOLATILE)) {
1626 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1627 printk(KERN_CONT " inactive");
1628 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
1629 printk(KERN_CONT " grabbed");
1630 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
1631 printk(KERN_CONT " volatile");
1632 }
1633 printk(KERN_CONT "\n");
Hans Verkuil09965172010-08-01 14:32:42 -03001634}
1635
1636/* Log all controls owned by the handler */
1637void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1638 const char *prefix)
1639{
1640 struct v4l2_ctrl *ctrl;
1641 const char *colon = "";
1642 int len;
1643
1644 if (hdl == NULL)
1645 return;
1646 if (prefix == NULL)
1647 prefix = "";
1648 len = strlen(prefix);
1649 if (len && prefix[len - 1] != ' ')
1650 colon = ": ";
1651 mutex_lock(&hdl->lock);
1652 list_for_each_entry(ctrl, &hdl->ctrls, node)
1653 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1654 log_ctrl(ctrl, prefix, colon);
1655 mutex_unlock(&hdl->lock);
1656}
1657EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1658
1659/* Call s_ctrl for all controls owned by the handler */
1660int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1661{
1662 struct v4l2_ctrl *ctrl;
1663 int ret = 0;
1664
1665 if (hdl == NULL)
1666 return 0;
1667 mutex_lock(&hdl->lock);
1668 list_for_each_entry(ctrl, &hdl->ctrls, node)
1669 ctrl->done = false;
1670
1671 list_for_each_entry(ctrl, &hdl->ctrls, node) {
1672 struct v4l2_ctrl *master = ctrl->cluster[0];
1673 int i;
1674
1675 /* Skip if this control was already handled by a cluster. */
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001676 /* Skip button controls and read-only controls. */
1677 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1678 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
Hans Verkuil09965172010-08-01 14:32:42 -03001679 continue;
1680
Hans Verkuil2a863792011-01-11 14:45:03 -03001681 for (i = 0; i < master->ncontrols; i++) {
1682 if (master->cluster[i]) {
1683 cur_to_new(master->cluster[i]);
1684 master->cluster[i]->is_new = 1;
Hans Verkuil71c6c4c2011-06-14 11:01:52 -03001685 master->cluster[i]->done = true;
Hans Verkuil2a863792011-01-11 14:45:03 -03001686 }
1687 }
Hans Verkuil54c911e2011-05-25 06:04:58 -03001688 ret = call_op(master, s_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03001689 if (ret)
1690 break;
Hans Verkuil09965172010-08-01 14:32:42 -03001691 }
1692 mutex_unlock(&hdl->lock);
1693 return ret;
1694}
1695EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1696
1697/* Implement VIDIOC_QUERYCTRL */
1698int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1699{
1700 u32 id = qc->id & V4L2_CTRL_ID_MASK;
1701 struct v4l2_ctrl_ref *ref;
1702 struct v4l2_ctrl *ctrl;
1703
1704 if (hdl == NULL)
1705 return -EINVAL;
1706
1707 mutex_lock(&hdl->lock);
1708
1709 /* Try to find it */
1710 ref = find_ref(hdl, id);
1711
1712 if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1713 /* Find the next control with ID > qc->id */
1714
1715 /* Did we reach the end of the control list? */
1716 if (id >= node2id(hdl->ctrl_refs.prev)) {
1717 ref = NULL; /* Yes, so there is no next control */
1718 } else if (ref) {
1719 /* We found a control with the given ID, so just get
1720 the next one in the list. */
1721 ref = list_entry(ref->node.next, typeof(*ref), node);
1722 } else {
1723 /* No control with the given ID exists, so start
1724 searching for the next largest ID. We know there
1725 is one, otherwise the first 'if' above would have
1726 been true. */
1727 list_for_each_entry(ref, &hdl->ctrl_refs, node)
1728 if (id < ref->ctrl->id)
1729 break;
1730 }
1731 }
1732 mutex_unlock(&hdl->lock);
1733 if (!ref)
1734 return -EINVAL;
1735
1736 ctrl = ref->ctrl;
1737 memset(qc, 0, sizeof(*qc));
Hans Verkuil829fb2d2011-01-16 11:21:40 -03001738 if (id >= V4L2_CID_PRIVATE_BASE)
1739 qc->id = id;
1740 else
1741 qc->id = ctrl->id;
Hans Verkuil09965172010-08-01 14:32:42 -03001742 strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1743 qc->minimum = ctrl->minimum;
1744 qc->maximum = ctrl->maximum;
1745 qc->default_value = ctrl->default_value;
Laurent Pincharteac9aa02010-12-07 08:57:25 -03001746 if (ctrl->type == V4L2_CTRL_TYPE_MENU)
Hans Verkuil09965172010-08-01 14:32:42 -03001747 qc->step = 1;
1748 else
1749 qc->step = ctrl->step;
1750 qc->flags = ctrl->flags;
1751 qc->type = ctrl->type;
1752 return 0;
1753}
1754EXPORT_SYMBOL(v4l2_queryctrl);
1755
1756int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1757{
Hans Verkuil87a0c942011-02-22 12:31:07 -03001758 if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1759 return -EINVAL;
Hans Verkuil09965172010-08-01 14:32:42 -03001760 return v4l2_queryctrl(sd->ctrl_handler, qc);
1761}
1762EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1763
1764/* Implement VIDIOC_QUERYMENU */
1765int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1766{
1767 struct v4l2_ctrl *ctrl;
1768 u32 i = qm->index;
1769
1770 ctrl = v4l2_ctrl_find(hdl, qm->id);
1771 if (!ctrl)
1772 return -EINVAL;
1773
1774 qm->reserved = 0;
1775 /* Sanity checks */
1776 if (ctrl->qmenu == NULL ||
1777 i < ctrl->minimum || i > ctrl->maximum)
1778 return -EINVAL;
1779 /* Use mask to see if this menu item should be skipped */
1780 if (ctrl->menu_skip_mask & (1 << i))
1781 return -EINVAL;
1782 /* Empty menu items should also be skipped */
1783 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1784 return -EINVAL;
1785 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1786 return 0;
1787}
1788EXPORT_SYMBOL(v4l2_querymenu);
1789
1790int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1791{
1792 return v4l2_querymenu(sd->ctrl_handler, qm);
1793}
1794EXPORT_SYMBOL(v4l2_subdev_querymenu);
1795
1796
1797
1798/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1799
1800 It is not a fully atomic operation, just best-effort only. After all, if
1801 multiple controls have to be set through multiple i2c writes (for example)
1802 then some initial writes may succeed while others fail. Thus leaving the
1803 system in an inconsistent state. The question is how much effort you are
1804 willing to spend on trying to make something atomic that really isn't.
1805
1806 From the point of view of an application the main requirement is that
1807 when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1808 error should be returned without actually affecting any controls.
1809
1810 If all the values are correct, then it is acceptable to just give up
1811 in case of low-level errors.
1812
1813 It is important though that the application can tell when only a partial
1814 configuration was done. The way we do that is through the error_idx field
1815 of struct v4l2_ext_controls: if that is equal to the count field then no
1816 controls were affected. Otherwise all controls before that index were
1817 successful in performing their 'get' or 'set' operation, the control at
1818 the given index failed, and you don't know what happened with the controls
1819 after the failed one. Since if they were part of a control cluster they
1820 could have been successfully processed (if a cluster member was encountered
1821 at index < error_idx), they could have failed (if a cluster member was at
1822 error_idx), or they may not have been processed yet (if the first cluster
1823 member appeared after error_idx).
1824
1825 It is all fairly theoretical, though. In practice all you can do is to
1826 bail out. If error_idx == count, then it is an application bug. If
1827 error_idx < count then it is only an application bug if the error code was
1828 EBUSY. That usually means that something started streaming just when you
1829 tried to set the controls. In all other cases it is a driver/hardware
1830 problem and all you can do is to retry or bail out.
1831
1832 Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1833 never modifies controls the error_idx is just set to whatever control
1834 has an invalid value.
1835 */
1836
1837/* Prepare for the extended g/s/try functions.
1838 Find the controls in the control array and do some basic checks. */
1839static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1840 struct v4l2_ext_controls *cs,
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001841 struct v4l2_ctrl_helper *helpers)
Hans Verkuil09965172010-08-01 14:32:42 -03001842{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001843 struct v4l2_ctrl_helper *h;
1844 bool have_clusters = false;
Hans Verkuil09965172010-08-01 14:32:42 -03001845 u32 i;
1846
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001847 for (i = 0, h = helpers; i < cs->count; i++, h++) {
Hans Verkuil09965172010-08-01 14:32:42 -03001848 struct v4l2_ext_control *c = &cs->controls[i];
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001849 struct v4l2_ctrl_ref *ref;
Hans Verkuil09965172010-08-01 14:32:42 -03001850 struct v4l2_ctrl *ctrl;
1851 u32 id = c->id & V4L2_CTRL_ID_MASK;
1852
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001853 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03001854
1855 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1856 return -EINVAL;
1857
1858 /* Old-style private controls are not allowed for
1859 extended controls */
1860 if (id >= V4L2_CID_PRIVATE_BASE)
1861 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001862 ref = find_ref_lock(hdl, id);
1863 if (ref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001864 return -EINVAL;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001865 ctrl = ref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001866 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1867 return -EINVAL;
1868
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001869 if (ctrl->cluster[0]->ncontrols > 1)
1870 have_clusters = true;
1871 if (ctrl->cluster[0] != ctrl)
1872 ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
1873 /* Store the ref to the master control of the cluster */
1874 h->mref = ref;
1875 h->ctrl = ctrl;
1876 /* Initially set next to 0, meaning that there is no other
1877 control in this helper array belonging to the same
1878 cluster */
1879 h->next = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03001880 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001881
1882 /* We are done if there were no controls that belong to a multi-
1883 control cluster. */
1884 if (!have_clusters)
1885 return 0;
1886
1887 /* The code below figures out in O(n) time which controls in the list
1888 belong to the same cluster. */
1889
1890 /* This has to be done with the handler lock taken. */
1891 mutex_lock(&hdl->lock);
1892
1893 /* First zero the helper field in the master control references */
1894 for (i = 0; i < cs->count; i++)
1895 helpers[i].mref->helper = 0;
1896 for (i = 0, h = helpers; i < cs->count; i++, h++) {
1897 struct v4l2_ctrl_ref *mref = h->mref;
1898
1899 /* If the mref->helper is set, then it points to an earlier
1900 helper that belongs to the same cluster. */
1901 if (mref->helper) {
1902 /* Set the next field of mref->helper to the current
1903 index: this means that that earlier helper now
1904 points to the next helper in the same cluster. */
1905 mref->helper->next = i;
1906 /* mref should be set only for the first helper in the
1907 cluster, clear the others. */
1908 h->mref = NULL;
1909 }
1910 /* Point the mref helper to the current helper struct. */
1911 mref->helper = h;
1912 }
1913 mutex_unlock(&hdl->lock);
Hans Verkuil09965172010-08-01 14:32:42 -03001914 return 0;
1915}
1916
Hans Verkuil09965172010-08-01 14:32:42 -03001917/* Handles the corner case where cs->count == 0. It checks whether the
1918 specified control class exists. If that class ID is 0, then it checks
1919 whether there are any controls at all. */
1920static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1921{
1922 if (ctrl_class == 0)
1923 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1924 return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1925}
1926
1927
1928
1929/* Get extended controls. Allocates the helpers array if needed. */
1930int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1931{
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001932 struct v4l2_ctrl_helper helper[4];
1933 struct v4l2_ctrl_helper *helpers = helper;
Hans Verkuil09965172010-08-01 14:32:42 -03001934 int ret;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001935 int i, j;
Hans Verkuil09965172010-08-01 14:32:42 -03001936
1937 cs->error_idx = cs->count;
1938 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1939
1940 if (hdl == NULL)
1941 return -EINVAL;
1942
1943 if (cs->count == 0)
1944 return class_check(hdl, cs->ctrl_class);
1945
1946 if (cs->count > ARRAY_SIZE(helper)) {
1947 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1948 if (helpers == NULL)
1949 return -ENOMEM;
1950 }
1951
Hans Verkuil37cd3b72011-06-07 04:40:04 -03001952 ret = prepare_ext_ctrls(hdl, cs, helpers);
1953 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03001954
1955 for (i = 0; !ret && i < cs->count; i++)
1956 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1957 ret = -EACCES;
1958
1959 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001960 int (*ctrl_to_user)(struct v4l2_ext_control *c,
1961 struct v4l2_ctrl *ctrl) = cur_to_user;
1962 struct v4l2_ctrl *master;
Hans Verkuil09965172010-08-01 14:32:42 -03001963
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001964 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03001965 continue;
1966
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001967 master = helpers[i].mref->ctrl;
Hans Verkuil09965172010-08-01 14:32:42 -03001968 cs->error_idx = i;
1969
1970 v4l2_ctrl_lock(master);
Hans Verkuilddac5c12011-06-10 05:43:34 -03001971
1972 /* g_volatile_ctrl will update the new control values */
Hans Verkuil5626b8c2011-08-26 07:53:53 -03001973 if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
1974 (master->has_volatiles && !is_cur_manual(master))) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03001975 for (j = 0; j < master->ncontrols; j++)
1976 cur_to_new(master->cluster[j]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03001977 ret = call_op(master, g_volatile_ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001978 ctrl_to_user = new_to_user;
Hans Verkuilddac5c12011-06-10 05:43:34 -03001979 }
1980 /* If OK, then copy the current (for non-volatile controls)
1981 or the new (for volatile controls) control values to the
1982 caller */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03001983 if (!ret) {
1984 u32 idx = i;
1985
1986 do {
1987 ret = ctrl_to_user(cs->controls + idx,
1988 helpers[idx].ctrl);
1989 idx = helpers[idx].next;
1990 } while (!ret && idx);
1991 }
Hans Verkuil09965172010-08-01 14:32:42 -03001992 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03001993 }
1994
1995 if (cs->count > ARRAY_SIZE(helper))
1996 kfree(helpers);
1997 return ret;
1998}
1999EXPORT_SYMBOL(v4l2_g_ext_ctrls);
2000
2001int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2002{
2003 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
2004}
2005EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
2006
2007/* Helper function to get a single control */
2008static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
2009{
2010 struct v4l2_ctrl *master = ctrl->cluster[0];
2011 int ret = 0;
Hans Verkuilddac5c12011-06-10 05:43:34 -03002012 int i;
Hans Verkuil09965172010-08-01 14:32:42 -03002013
2014 if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2015 return -EACCES;
2016
2017 v4l2_ctrl_lock(master);
2018 /* g_volatile_ctrl will update the current control values */
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002019 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
Hans Verkuilddac5c12011-06-10 05:43:34 -03002020 for (i = 0; i < master->ncontrols; i++)
2021 cur_to_new(master->cluster[i]);
Hans Verkuil54c911e2011-05-25 06:04:58 -03002022 ret = call_op(master, g_volatile_ctrl);
Hans Verkuilddac5c12011-06-10 05:43:34 -03002023 *val = ctrl->val;
2024 } else {
2025 *val = ctrl->cur.val;
2026 }
Hans Verkuil09965172010-08-01 14:32:42 -03002027 v4l2_ctrl_unlock(master);
2028 return ret;
2029}
2030
2031int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
2032{
2033 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2034
2035 if (ctrl == NULL || !type_is_int(ctrl))
2036 return -EINVAL;
2037 return get_ctrl(ctrl, &control->value);
2038}
2039EXPORT_SYMBOL(v4l2_g_ctrl);
2040
2041int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2042{
2043 return v4l2_g_ctrl(sd->ctrl_handler, control);
2044}
2045EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
2046
2047s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
2048{
2049 s32 val = 0;
2050
2051 /* It's a driver bug if this happens. */
2052 WARN_ON(!type_is_int(ctrl));
2053 get_ctrl(ctrl, &val);
2054 return val;
2055}
2056EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
2057
2058
2059/* Core function that calls try/s_ctrl and ensures that the new value is
2060 copied to the current value on a set.
2061 Must be called with ctrl->handler->lock held. */
Hans Verkuile6402582011-06-14 10:56:42 -03002062static int try_or_set_cluster(struct v4l2_fh *fh,
2063 struct v4l2_ctrl *master, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03002064{
Hans Verkuil72d877c2011-06-10 05:44:36 -03002065 bool update_flag;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002066 int ret;
Hans Verkuil09965172010-08-01 14:32:42 -03002067 int i;
2068
2069 /* Go through the cluster and either validate the new value or
2070 (if no new value was set), copy the current value to the new
2071 value, ensuring a consistent view for the control ops when
2072 called. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002073 for (i = 0; i < master->ncontrols; i++) {
Hans Verkuil09965172010-08-01 14:32:42 -03002074 struct v4l2_ctrl *ctrl = master->cluster[i];
2075
2076 if (ctrl == NULL)
2077 continue;
2078
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002079 if (!ctrl->is_new) {
2080 cur_to_new(ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03002081 continue;
2082 }
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002083 /* Check again: it may have changed since the
2084 previous check in try_or_set_ext_ctrls(). */
2085 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2086 return -EBUSY;
Hans Verkuil09965172010-08-01 14:32:42 -03002087 }
2088
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002089 ret = call_op(master, try_ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -03002090
2091 /* Don't set if there is no change */
Hans Verkuil72d877c2011-06-10 05:44:36 -03002092 if (ret || !set || !cluster_changed(master))
2093 return ret;
2094 ret = call_op(master, s_ctrl);
Hans Verkuil72d877c2011-06-10 05:44:36 -03002095 if (ret)
2096 return ret;
2097
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002098 /* If OK, then make the new values permanent. */
Hans Verkuil72d877c2011-06-10 05:44:36 -03002099 update_flag = is_cur_manual(master) != is_new_manual(master);
2100 for (i = 0; i < master->ncontrols; i++)
Hans Verkuilab892ba2011-06-07 06:47:18 -03002101 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
Hans Verkuil72d877c2011-06-10 05:44:36 -03002102 return 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002103}
2104
Hans Verkuile6402582011-06-14 10:56:42 -03002105/* Validate controls. */
2106static int validate_ctrls(struct v4l2_ext_controls *cs,
2107 struct v4l2_ctrl_helper *helpers, bool set)
Hans Verkuil09965172010-08-01 14:32:42 -03002108{
Hans Verkuile6402582011-06-14 10:56:42 -03002109 unsigned i;
Hans Verkuil09965172010-08-01 14:32:42 -03002110 int ret = 0;
2111
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002112 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03002113 for (i = 0; i < cs->count; i++) {
2114 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
2115
Hans Verkuile6402582011-06-14 10:56:42 -03002116 cs->error_idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03002117
2118 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2119 return -EACCES;
2120 /* This test is also done in try_set_control_cluster() which
2121 is called in atomic context, so that has the final say,
2122 but it makes sense to do an up-front check as well. Once
2123 an error occurs in try_set_control_cluster() some other
2124 controls may have been set already and we want to do a
2125 best-effort to avoid that. */
2126 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2127 return -EBUSY;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002128 ret = validate_new(ctrl, &cs->controls[i]);
2129 if (ret)
2130 return ret;
Hans Verkuil09965172010-08-01 14:32:42 -03002131 }
Hans Verkuile6402582011-06-14 10:56:42 -03002132 return 0;
2133}
Hans Verkuil09965172010-08-01 14:32:42 -03002134
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002135/* Obtain the current volatile values of an autocluster and mark them
2136 as new. */
2137static void update_from_auto_cluster(struct v4l2_ctrl *master)
2138{
2139 int i;
2140
2141 for (i = 0; i < master->ncontrols; i++)
2142 cur_to_new(master->cluster[i]);
2143 if (!call_op(master, g_volatile_ctrl))
2144 for (i = 1; i < master->ncontrols; i++)
2145 if (master->cluster[i])
2146 master->cluster[i]->is_new = 1;
2147}
2148
Hans Verkuile6402582011-06-14 10:56:42 -03002149/* Try or try-and-set controls */
2150static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2151 struct v4l2_ext_controls *cs,
2152 bool set)
2153{
2154 struct v4l2_ctrl_helper helper[4];
2155 struct v4l2_ctrl_helper *helpers = helper;
2156 unsigned i, j;
2157 int ret;
2158
2159 cs->error_idx = cs->count;
2160 cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2161
2162 if (hdl == NULL)
2163 return -EINVAL;
2164
2165 if (cs->count == 0)
2166 return class_check(hdl, cs->ctrl_class);
2167
2168 if (cs->count > ARRAY_SIZE(helper)) {
2169 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
2170 if (!helpers)
2171 return -ENOMEM;
2172 }
2173 ret = prepare_ext_ctrls(hdl, cs, helpers);
2174 if (!ret)
2175 ret = validate_ctrls(cs, helpers, set);
2176 if (ret && set)
2177 cs->error_idx = cs->count;
Hans Verkuil09965172010-08-01 14:32:42 -03002178 for (i = 0; !ret && i < cs->count; i++) {
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002179 struct v4l2_ctrl *master;
2180 u32 idx = i;
Hans Verkuil09965172010-08-01 14:32:42 -03002181
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002182 if (helpers[i].mref == NULL)
Hans Verkuil09965172010-08-01 14:32:42 -03002183 continue;
2184
Hans Verkuil37cd3b72011-06-07 04:40:04 -03002185 cs->error_idx = i;
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002186 master = helpers[i].mref->ctrl;
2187 v4l2_ctrl_lock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002188
Hans Verkuil2a863792011-01-11 14:45:03 -03002189 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002190 for (j = 0; j < master->ncontrols; j++)
2191 if (master->cluster[j])
Hans Verkuil2a863792011-01-11 14:45:03 -03002192 master->cluster[j]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002193
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002194 /* For volatile autoclusters that are currently in auto mode
2195 we need to discover if it will be set to manual mode.
2196 If so, then we have to copy the current volatile values
2197 first since those will become the new manual values (which
2198 may be overwritten by explicit new values from this set
2199 of controls). */
2200 if (master->is_auto && master->has_volatiles &&
2201 !is_cur_manual(master)) {
2202 /* Pick an initial non-manual value */
2203 s32 new_auto_val = master->manual_mode_value + 1;
2204 u32 tmp_idx = idx;
2205
2206 do {
2207 /* Check if the auto control is part of the
2208 list, and remember the new value. */
2209 if (helpers[tmp_idx].ctrl == master)
2210 new_auto_val = cs->controls[tmp_idx].value;
2211 tmp_idx = helpers[tmp_idx].next;
2212 } while (tmp_idx);
2213 /* If the new value == the manual value, then copy
2214 the current volatile values. */
2215 if (new_auto_val == master->manual_mode_value)
2216 update_from_auto_cluster(master);
2217 }
2218
Hans Verkuil09965172010-08-01 14:32:42 -03002219 /* Copy the new caller-supplied control values.
Hans Verkuil2a863792011-01-11 14:45:03 -03002220 user_to_new() sets 'is_new' to 1. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002221 do {
2222 ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
2223 idx = helpers[idx].next;
2224 } while (!ret && idx);
Hans Verkuil09965172010-08-01 14:32:42 -03002225
2226 if (!ret)
Hans Verkuile6402582011-06-14 10:56:42 -03002227 ret = try_or_set_cluster(fh, master, set);
Hans Verkuil09965172010-08-01 14:32:42 -03002228
2229 /* Copy the new values back to userspace. */
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002230 if (!ret) {
2231 idx = i;
2232 do {
Hans Verkuiladf41b92011-07-05 06:56:37 -03002233 ret = new_to_user(cs->controls + idx,
Hans Verkuile6402582011-06-14 10:56:42 -03002234 helpers[idx].ctrl);
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002235 idx = helpers[idx].next;
2236 } while (!ret && idx);
2237 }
2238 v4l2_ctrl_unlock(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002239 }
Hans Verkuil09965172010-08-01 14:32:42 -03002240
Hans Verkuil09965172010-08-01 14:32:42 -03002241 if (cs->count > ARRAY_SIZE(helper))
2242 kfree(helpers);
2243 return ret;
2244}
2245
2246int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2247{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002248 return try_set_ext_ctrls(NULL, hdl, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002249}
2250EXPORT_SYMBOL(v4l2_try_ext_ctrls);
2251
Hans Verkuilab892ba2011-06-07 06:47:18 -03002252int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2253 struct v4l2_ext_controls *cs)
Hans Verkuil09965172010-08-01 14:32:42 -03002254{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002255 return try_set_ext_ctrls(fh, hdl, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002256}
2257EXPORT_SYMBOL(v4l2_s_ext_ctrls);
2258
2259int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2260{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002261 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
Hans Verkuil09965172010-08-01 14:32:42 -03002262}
2263EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
2264
2265int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2266{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002267 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002268}
2269EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
2270
2271/* Helper function for VIDIOC_S_CTRL compatibility */
Hans Verkuilab892ba2011-06-07 06:47:18 -03002272static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
Hans Verkuil09965172010-08-01 14:32:42 -03002273{
2274 struct v4l2_ctrl *master = ctrl->cluster[0];
2275 int ret;
2276 int i;
2277
Hans Verkuileb5b16e2011-06-14 10:04:06 -03002278 ret = validate_new_int(ctrl, val);
2279 if (ret)
2280 return ret;
2281
Hans Verkuil09965172010-08-01 14:32:42 -03002282 v4l2_ctrl_lock(ctrl);
2283
Hans Verkuil2a863792011-01-11 14:45:03 -03002284 /* Reset the 'is_new' flags of the cluster */
Hans Verkuil09965172010-08-01 14:32:42 -03002285 for (i = 0; i < master->ncontrols; i++)
2286 if (master->cluster[i])
Hans Verkuil2a863792011-01-11 14:45:03 -03002287 master->cluster[i]->is_new = 0;
Hans Verkuil09965172010-08-01 14:32:42 -03002288
Hans Verkuil5626b8c2011-08-26 07:53:53 -03002289 /* For autoclusters with volatiles that are switched from auto to
2290 manual mode we have to update the current volatile values since
2291 those will become the initial manual values after such a switch. */
2292 if (master->is_auto && master->has_volatiles && ctrl == master &&
2293 !is_cur_manual(master) && *val == master->manual_mode_value)
2294 update_from_auto_cluster(master);
Hans Verkuil09965172010-08-01 14:32:42 -03002295 ctrl->val = *val;
Hans Verkuil2a863792011-01-11 14:45:03 -03002296 ctrl->is_new = 1;
Hans Verkuile6402582011-06-14 10:56:42 -03002297 ret = try_or_set_cluster(fh, master, true);
Hans Verkuil09965172010-08-01 14:32:42 -03002298 *val = ctrl->cur.val;
2299 v4l2_ctrl_unlock(ctrl);
2300 return ret;
2301}
2302
Hans Verkuilab892ba2011-06-07 06:47:18 -03002303int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2304 struct v4l2_control *control)
Hans Verkuil09965172010-08-01 14:32:42 -03002305{
2306 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2307
2308 if (ctrl == NULL || !type_is_int(ctrl))
2309 return -EINVAL;
2310
Hans Verkuil7ebbc392011-06-07 04:50:31 -03002311 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2312 return -EACCES;
2313
Hans Verkuilab892ba2011-06-07 06:47:18 -03002314 return set_ctrl(fh, ctrl, &control->value);
Hans Verkuil09965172010-08-01 14:32:42 -03002315}
2316EXPORT_SYMBOL(v4l2_s_ctrl);
2317
2318int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2319{
Hans Verkuilab892ba2011-06-07 06:47:18 -03002320 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
Hans Verkuil09965172010-08-01 14:32:42 -03002321}
2322EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2323
2324int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2325{
2326 /* It's a driver bug if this happens. */
2327 WARN_ON(!type_is_int(ctrl));
Hans Verkuilab892ba2011-06-07 06:47:18 -03002328 return set_ctrl(NULL, ctrl, &val);
Hans Verkuil09965172010-08-01 14:32:42 -03002329}
2330EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
Hans Verkuil6e239392011-06-07 11:13:44 -03002331
Hans Verkuil77068d32011-06-13 18:55:58 -03002332void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
2333 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002334{
Hans Verkuil6e239392011-06-07 11:13:44 -03002335 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002336 list_add_tail(&sev->node, &ctrl->ev_subs);
Hans Verkuil6e239392011-06-07 11:13:44 -03002337 if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
Hans Verkuil77068d32011-06-13 18:55:58 -03002338 (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
Hans Verkuil6e239392011-06-07 11:13:44 -03002339 struct v4l2_event ev;
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002340 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
Hans Verkuil6e239392011-06-07 11:13:44 -03002341
Hans Verkuilc12fcfd2011-06-14 02:42:45 -03002342 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2343 changes |= V4L2_EVENT_CTRL_CH_VALUE;
2344 fill_event(&ev, ctrl, changes);
Hans Verkuil77068d32011-06-13 18:55:58 -03002345 v4l2_event_queue_fh(sev->fh, &ev);
Hans Verkuil6e239392011-06-07 11:13:44 -03002346 }
2347 v4l2_ctrl_unlock(ctrl);
2348}
Hans Verkuil77068d32011-06-13 18:55:58 -03002349EXPORT_SYMBOL(v4l2_ctrl_add_event);
Hans Verkuil6e239392011-06-07 11:13:44 -03002350
Hans Verkuil77068d32011-06-13 18:55:58 -03002351void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
2352 struct v4l2_subscribed_event *sev)
Hans Verkuil6e239392011-06-07 11:13:44 -03002353{
Hans Verkuil6e239392011-06-07 11:13:44 -03002354 v4l2_ctrl_lock(ctrl);
Hans Verkuil77068d32011-06-13 18:55:58 -03002355 list_del(&sev->node);
Hans Verkuil6e239392011-06-07 11:13:44 -03002356 v4l2_ctrl_unlock(ctrl);
2357}
Hans Verkuil77068d32011-06-13 18:55:58 -03002358EXPORT_SYMBOL(v4l2_ctrl_del_event);