Initial commit
[kernel/linux-3.0.git] / drivers / media / video / v4l2-ctrls.c
1 /*
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>
22 #include <linux/slab.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-dev.h>
27
28 /* Internal temporary helper struct, one for each v4l2_ext_control */
29 struct ctrl_helper {
30         /* The control corresponding to the v4l2_ext_control ID field. */
31         struct v4l2_ctrl *ctrl;
32         /* Used internally to mark whether this control was already
33            processed. */
34         bool handled;
35 };
36
37 /* Returns NULL or a character pointer array containing the menu for
38    the given control ID. The pointer array ends with a NULL pointer.
39    An empty string signifies a menu entry that is invalid. This allows
40    drivers to disable certain options if it is not supported. */
41 const char * const *v4l2_ctrl_get_menu(u32 id)
42 {
43         static const char * const mpeg_audio_sampling_freq[] = {
44                 "44.1 kHz",
45                 "48 kHz",
46                 "32 kHz",
47                 NULL
48         };
49         static const char * const mpeg_audio_encoding[] = {
50                 "MPEG-1/2 Layer I",
51                 "MPEG-1/2 Layer II",
52                 "MPEG-1/2 Layer III",
53                 "MPEG-2/4 AAC",
54                 "AC-3",
55                 NULL
56         };
57         static const char * const mpeg_audio_l1_bitrate[] = {
58                 "32 kbps",
59                 "64 kbps",
60                 "96 kbps",
61                 "128 kbps",
62                 "160 kbps",
63                 "192 kbps",
64                 "224 kbps",
65                 "256 kbps",
66                 "288 kbps",
67                 "320 kbps",
68                 "352 kbps",
69                 "384 kbps",
70                 "416 kbps",
71                 "448 kbps",
72                 NULL
73         };
74         static const char * const mpeg_audio_l2_bitrate[] = {
75                 "32 kbps",
76                 "48 kbps",
77                 "56 kbps",
78                 "64 kbps",
79                 "80 kbps",
80                 "96 kbps",
81                 "112 kbps",
82                 "128 kbps",
83                 "160 kbps",
84                 "192 kbps",
85                 "224 kbps",
86                 "256 kbps",
87                 "320 kbps",
88                 "384 kbps",
89                 NULL
90         };
91         static const char * const mpeg_audio_l3_bitrate[] = {
92                 "32 kbps",
93                 "40 kbps",
94                 "48 kbps",
95                 "56 kbps",
96                 "64 kbps",
97                 "80 kbps",
98                 "96 kbps",
99                 "112 kbps",
100                 "128 kbps",
101                 "160 kbps",
102                 "192 kbps",
103                 "224 kbps",
104                 "256 kbps",
105                 "320 kbps",
106                 NULL
107         };
108         static const char * const mpeg_audio_ac3_bitrate[] = {
109                 "32 kbps",
110                 "40 kbps",
111                 "48 kbps",
112                 "56 kbps",
113                 "64 kbps",
114                 "80 kbps",
115                 "96 kbps",
116                 "112 kbps",
117                 "128 kbps",
118                 "160 kbps",
119                 "192 kbps",
120                 "224 kbps",
121                 "256 kbps",
122                 "320 kbps",
123                 "384 kbps",
124                 "448 kbps",
125                 "512 kbps",
126                 "576 kbps",
127                 "640 kbps",
128                 NULL
129         };
130         static const char * const mpeg_audio_mode[] = {
131                 "Stereo",
132                 "Joint Stereo",
133                 "Dual",
134                 "Mono",
135                 NULL
136         };
137         static const char * const mpeg_audio_mode_extension[] = {
138                 "Bound 4",
139                 "Bound 8",
140                 "Bound 12",
141                 "Bound 16",
142                 NULL
143         };
144         static const char * const mpeg_audio_emphasis[] = {
145                 "No Emphasis",
146                 "50/15 us",
147                 "CCITT J17",
148                 NULL
149         };
150         static const char * const mpeg_audio_crc[] = {
151                 "No CRC",
152                 "16-bit CRC",
153                 NULL
154         };
155         static const char * const mpeg_video_encoding[] = {
156                 "MPEG-1",
157                 "MPEG-2",
158                 "MPEG-4 AVC",
159                 NULL
160         };
161         static const char * const mpeg_video_aspect[] = {
162                 "1x1",
163                 "4x3",
164                 "16x9",
165                 "2.21x1",
166                 NULL
167         };
168         static const char * const mpeg_video_bitrate_mode[] = {
169                 "Variable Bitrate",
170                 "Constant Bitrate",
171                 NULL
172         };
173         static const char * const mpeg_stream_type[] = {
174                 "MPEG-2 Program Stream",
175                 "MPEG-2 Transport Stream",
176                 "MPEG-1 System Stream",
177                 "MPEG-2 DVD-compatible Stream",
178                 "MPEG-1 VCD-compatible Stream",
179                 "MPEG-2 SVCD-compatible Stream",
180                 NULL
181         };
182         static const char * const mpeg_stream_vbi_fmt[] = {
183                 "No VBI",
184                 "Private packet, IVTV format",
185                 NULL
186         };
187         static const char * const camera_power_line_frequency[] = {
188                 "Disabled",
189                 "50 Hz",
190                 "60 Hz",
191                 NULL
192         };
193         static const char * const camera_exposure_auto[] = {
194                 "Auto Mode",
195                 "Manual Mode",
196                 "Shutter Priority Mode",
197                 "Aperture Priority Mode",
198                 NULL
199         };
200         static const char * const colorfx[] = {
201                 "None",
202                 "Black & White",
203                 "Sepia",
204                 "Negative",
205                 "Emboss",
206                 "Sketch",
207                 "Sky blue",
208                 "Grass green",
209                 "Skin whiten",
210                 "Vivid",
211                 NULL
212         };
213         static const char * const tune_preemphasis[] = {
214                 "No preemphasis",
215                 "50 useconds",
216                 "75 useconds",
217                 NULL,
218         };
219         static const char * const tune_deemphasis[] = {
220                 "No Preemphasis",
221                 "50 useconds",
222                 "75 useconds",
223                 NULL,
224         };
225
226         switch (id) {
227         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
228                 return mpeg_audio_sampling_freq;
229         case V4L2_CID_MPEG_AUDIO_ENCODING:
230                 return mpeg_audio_encoding;
231         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
232                 return mpeg_audio_l1_bitrate;
233         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
234                 return mpeg_audio_l2_bitrate;
235         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
236                 return mpeg_audio_l3_bitrate;
237         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
238                 return mpeg_audio_ac3_bitrate;
239         case V4L2_CID_MPEG_AUDIO_MODE:
240                 return mpeg_audio_mode;
241         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
242                 return mpeg_audio_mode_extension;
243         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
244                 return mpeg_audio_emphasis;
245         case V4L2_CID_MPEG_AUDIO_CRC:
246                 return mpeg_audio_crc;
247         case V4L2_CID_MPEG_VIDEO_ENCODING:
248                 return mpeg_video_encoding;
249         case V4L2_CID_MPEG_VIDEO_ASPECT:
250                 return mpeg_video_aspect;
251         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
252                 return mpeg_video_bitrate_mode;
253         case V4L2_CID_MPEG_STREAM_TYPE:
254                 return mpeg_stream_type;
255         case V4L2_CID_MPEG_STREAM_VBI_FMT:
256                 return mpeg_stream_vbi_fmt;
257         case V4L2_CID_POWER_LINE_FREQUENCY:
258                 return camera_power_line_frequency;
259         case V4L2_CID_EXPOSURE_AUTO:
260                 return camera_exposure_auto;
261         case V4L2_CID_COLORFX:
262                 return colorfx;
263         case V4L2_CID_TUNE_PREEMPHASIS:
264                 return tune_preemphasis;
265         case V4L2_CID_TUNE_DEEMPHASIS:
266                 return tune_deemphasis;
267         default:
268                 return NULL;
269         }
270 }
271 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
272
273 /* Return the control name. */
274 const char *v4l2_ctrl_get_name(u32 id)
275 {
276         switch (id) {
277         /* USER controls */
278         /* Keep the order of the 'case's the same as in videodev2.h! */
279         case V4L2_CID_USER_CLASS:               return "User Controls";
280         case V4L2_CID_BRIGHTNESS:               return "Brightness";
281         case V4L2_CID_CONTRAST:                 return "Contrast";
282         case V4L2_CID_SATURATION:               return "Saturation";
283         case V4L2_CID_HUE:                      return "Hue";
284         case V4L2_CID_AUDIO_VOLUME:             return "Volume";
285         case V4L2_CID_AUDIO_BALANCE:            return "Balance";
286         case V4L2_CID_AUDIO_BASS:               return "Bass";
287         case V4L2_CID_AUDIO_TREBLE:             return "Treble";
288         case V4L2_CID_AUDIO_MUTE:               return "Mute";
289         case V4L2_CID_AUDIO_LOUDNESS:           return "Loudness";
290         case V4L2_CID_BLACK_LEVEL:              return "Black Level";
291         case V4L2_CID_AUTO_WHITE_BALANCE:       return "White Balance, Automatic";
292         case V4L2_CID_DO_WHITE_BALANCE:         return "Do White Balance";
293         case V4L2_CID_RED_BALANCE:              return "Red Balance";
294         case V4L2_CID_BLUE_BALANCE:             return "Blue Balance";
295         case V4L2_CID_GAMMA:                    return "Gamma";
296         case V4L2_CID_EXPOSURE:                 return "Exposure";
297         case V4L2_CID_AUTOGAIN:                 return "Gain, Automatic";
298         case V4L2_CID_GAIN:                     return "Gain";
299         case V4L2_CID_HFLIP:                    return "Horizontal Flip";
300         case V4L2_CID_VFLIP:                    return "Vertical Flip";
301         case V4L2_CID_HCENTER:                  return "Horizontal Center";
302         case V4L2_CID_VCENTER:                  return "Vertical Center";
303         case V4L2_CID_POWER_LINE_FREQUENCY:     return "Power Line Frequency";
304         case V4L2_CID_HUE_AUTO:                 return "Hue, Automatic";
305         case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
306         case V4L2_CID_SHARPNESS:                return "Sharpness";
307         case V4L2_CID_BACKLIGHT_COMPENSATION:   return "Backlight Compensation";
308         case V4L2_CID_CHROMA_AGC:               return "Chroma AGC";
309         case V4L2_CID_COLOR_KILLER:             return "Color Killer";
310         case V4L2_CID_COLORFX:                  return "Color Effects";
311         case V4L2_CID_AUTOBRIGHTNESS:           return "Brightness, Automatic";
312         case V4L2_CID_BAND_STOP_FILTER:         return "Band-Stop Filter";
313         case V4L2_CID_ROTATE:                   return "Rotate";
314         case V4L2_CID_BG_COLOR:                 return "Background Color";
315         case V4L2_CID_CHROMA_GAIN:              return "Chroma Gain";
316         case V4L2_CID_ILLUMINATORS_1:           return "Illuminator 1";
317         case V4L2_CID_ILLUMINATORS_2:           return "Illuminator 2";
318
319         /* MPEG controls */
320         /* Keep the order of the 'case's the same as in videodev2.h! */
321         case V4L2_CID_MPEG_CLASS:               return "MPEG Encoder Controls";
322         case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
323         case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
324         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
325         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
326         case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
327         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
328         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
329         case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
330         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
331         case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
332         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
333         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
334         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
335         case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
336         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
337         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
338         case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
339         case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
340         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
341         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
342         case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
343         case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
344         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
345         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
346         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
347         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
348         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
349         case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
350         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
351         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
352         case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
353         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
354
355         /* CAMERA controls */
356         /* Keep the order of the 'case's the same as in videodev2.h! */
357         case V4L2_CID_CAMERA_CLASS:             return "Camera Controls";
358         case V4L2_CID_EXPOSURE_AUTO:            return "Auto Exposure";
359         case V4L2_CID_EXPOSURE_ABSOLUTE:        return "Exposure Time, Absolute";
360         case V4L2_CID_EXPOSURE_AUTO_PRIORITY:   return "Exposure, Dynamic Framerate";
361         case V4L2_CID_PAN_RELATIVE:             return "Pan, Relative";
362         case V4L2_CID_TILT_RELATIVE:            return "Tilt, Relative";
363         case V4L2_CID_PAN_RESET:                return "Pan, Reset";
364         case V4L2_CID_TILT_RESET:               return "Tilt, Reset";
365         case V4L2_CID_PAN_ABSOLUTE:             return "Pan, Absolute";
366         case V4L2_CID_TILT_ABSOLUTE:            return "Tilt, Absolute";
367         case V4L2_CID_FOCUS_ABSOLUTE:           return "Focus, Absolute";
368         case V4L2_CID_FOCUS_RELATIVE:           return "Focus, Relative";
369         case V4L2_CID_FOCUS_AUTO:               return "Focus, Automatic";
370         case V4L2_CID_ZOOM_ABSOLUTE:            return "Zoom, Absolute";
371         case V4L2_CID_ZOOM_RELATIVE:            return "Zoom, Relative";
372         case V4L2_CID_ZOOM_CONTINUOUS:          return "Zoom, Continuous";
373         case V4L2_CID_PRIVACY:                  return "Privacy";
374         case V4L2_CID_IRIS_ABSOLUTE:            return "Iris, Absolute";
375         case V4L2_CID_IRIS_RELATIVE:            return "Iris, Relative";
376
377         /* FM Radio Modulator control */
378         /* Keep the order of the 'case's the same as in videodev2.h! */
379         case V4L2_CID_FM_TX_CLASS:              return "FM Radio Modulator Controls";
380         case V4L2_CID_RDS_TX_DEVIATION:         return "RDS Signal Deviation";
381         case V4L2_CID_RDS_TX_PI:                return "RDS Program ID";
382         case V4L2_CID_RDS_TX_PTY:               return "RDS Program Type";
383         case V4L2_CID_RDS_TX_PS_NAME:           return "RDS PS Name";
384         case V4L2_CID_RDS_TX_RADIO_TEXT:        return "RDS Radio Text";
385         case V4L2_CID_AUDIO_LIMITER_ENABLED:    return "Audio Limiter Feature Enabled";
386         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
387         case V4L2_CID_AUDIO_LIMITER_DEVIATION:  return "Audio Limiter Deviation";
388         case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
389         case V4L2_CID_AUDIO_COMPRESSION_GAIN:   return "Audio Compression Gain";
390         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
391         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
392         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
393         case V4L2_CID_PILOT_TONE_ENABLED:       return "Pilot Tone Feature Enabled";
394         case V4L2_CID_PILOT_TONE_DEVIATION:     return "Pilot Tone Deviation";
395         case V4L2_CID_PILOT_TONE_FREQUENCY:     return "Pilot Tone Frequency";
396         case V4L2_CID_TUNE_PREEMPHASIS:         return "Pre-emphasis settings";
397         case V4L2_CID_TUNE_POWER_LEVEL:         return "Tune Power Level";
398         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:   return "Tune Antenna Capacitor";
399
400         /* FM Radio Tuner control */
401         /* Keep the order of the 'case's the same as in videodev2.h! */
402         case V4L2_CID_FM_RX_CLASS:      return "FM Radio Tuner Controls";
403         case V4L2_CID_TUNE_DEEMPHASIS:  return "De-emphasis settings";
404
405         default:
406                 return NULL;
407         }
408 }
409 EXPORT_SYMBOL(v4l2_ctrl_get_name);
410
411 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
412                     s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
413 {
414         *name = v4l2_ctrl_get_name(id);
415         *flags = 0;
416
417         switch (id) {
418         case V4L2_CID_AUDIO_MUTE:
419         case V4L2_CID_AUDIO_LOUDNESS:
420         case V4L2_CID_AUTO_WHITE_BALANCE:
421         case V4L2_CID_AUTOGAIN:
422         case V4L2_CID_HFLIP:
423         case V4L2_CID_VFLIP:
424         case V4L2_CID_HUE_AUTO:
425         case V4L2_CID_CHROMA_AGC:
426         case V4L2_CID_COLOR_KILLER:
427         case V4L2_CID_MPEG_AUDIO_MUTE:
428         case V4L2_CID_MPEG_VIDEO_MUTE:
429         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
430         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
431         case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
432         case V4L2_CID_FOCUS_AUTO:
433         case V4L2_CID_PRIVACY:
434         case V4L2_CID_AUDIO_LIMITER_ENABLED:
435         case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
436         case V4L2_CID_PILOT_TONE_ENABLED:
437         case V4L2_CID_ILLUMINATORS_1:
438         case V4L2_CID_ILLUMINATORS_2:
439                 *type = V4L2_CTRL_TYPE_BOOLEAN;
440                 *min = 0;
441                 *max = *step = 1;
442                 break;
443         case V4L2_CID_PAN_RESET:
444         case V4L2_CID_TILT_RESET:
445                 *type = V4L2_CTRL_TYPE_BUTTON;
446                 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
447                 *min = *max = *step = *def = 0;
448                 break;
449         case V4L2_CID_POWER_LINE_FREQUENCY:
450         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
451         case V4L2_CID_MPEG_AUDIO_ENCODING:
452         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
453         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
454         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
455         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
456         case V4L2_CID_MPEG_AUDIO_MODE:
457         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
458         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
459         case V4L2_CID_MPEG_AUDIO_CRC:
460         case V4L2_CID_MPEG_VIDEO_ENCODING:
461         case V4L2_CID_MPEG_VIDEO_ASPECT:
462         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
463         case V4L2_CID_MPEG_STREAM_TYPE:
464         case V4L2_CID_MPEG_STREAM_VBI_FMT:
465         case V4L2_CID_EXPOSURE_AUTO:
466         case V4L2_CID_COLORFX:
467         case V4L2_CID_TUNE_PREEMPHASIS:
468         case V4L2_CID_TUNE_DEEMPHASIS:
469                 *type = V4L2_CTRL_TYPE_MENU;
470                 break;
471         case V4L2_CID_RDS_TX_PS_NAME:
472         case V4L2_CID_RDS_TX_RADIO_TEXT:
473                 *type = V4L2_CTRL_TYPE_STRING;
474                 break;
475         case V4L2_CID_USER_CLASS:
476         case V4L2_CID_CAMERA_CLASS:
477         case V4L2_CID_MPEG_CLASS:
478         case V4L2_CID_FM_TX_CLASS:
479         case V4L2_CID_FM_RX_CLASS:
480                 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
481                 /* You can neither read not write these */
482                 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
483                 *min = *max = *step = *def = 0;
484                 break;
485         case V4L2_CID_BG_COLOR:
486                 *type = V4L2_CTRL_TYPE_INTEGER;
487                 *step = 1;
488                 *min = 0;
489                 /* Max is calculated as RGB888 that is 2^24 */
490                 *max = 0xFFFFFF;
491                 break;
492         default:
493                 *type = V4L2_CTRL_TYPE_INTEGER;
494                 break;
495         }
496         switch (id) {
497         case V4L2_CID_MPEG_AUDIO_ENCODING:
498         case V4L2_CID_MPEG_AUDIO_MODE:
499         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
500         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
501         case V4L2_CID_MPEG_STREAM_TYPE:
502                 *flags |= V4L2_CTRL_FLAG_UPDATE;
503                 break;
504         case V4L2_CID_AUDIO_VOLUME:
505         case V4L2_CID_AUDIO_BALANCE:
506         case V4L2_CID_AUDIO_BASS:
507         case V4L2_CID_AUDIO_TREBLE:
508         case V4L2_CID_BRIGHTNESS:
509         case V4L2_CID_CONTRAST:
510         case V4L2_CID_SATURATION:
511         case V4L2_CID_HUE:
512         case V4L2_CID_RED_BALANCE:
513         case V4L2_CID_BLUE_BALANCE:
514         case V4L2_CID_GAMMA:
515         case V4L2_CID_SHARPNESS:
516         case V4L2_CID_CHROMA_GAIN:
517         case V4L2_CID_RDS_TX_DEVIATION:
518         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
519         case V4L2_CID_AUDIO_LIMITER_DEVIATION:
520         case V4L2_CID_AUDIO_COMPRESSION_GAIN:
521         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
522         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
523         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
524         case V4L2_CID_PILOT_TONE_DEVIATION:
525         case V4L2_CID_PILOT_TONE_FREQUENCY:
526         case V4L2_CID_TUNE_POWER_LEVEL:
527         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
528                 *flags |= V4L2_CTRL_FLAG_SLIDER;
529                 break;
530         case V4L2_CID_PAN_RELATIVE:
531         case V4L2_CID_TILT_RELATIVE:
532         case V4L2_CID_FOCUS_RELATIVE:
533         case V4L2_CID_IRIS_RELATIVE:
534         case V4L2_CID_ZOOM_RELATIVE:
535                 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
536                 break;
537         }
538 }
539 EXPORT_SYMBOL(v4l2_ctrl_fill);
540
541 /* Helper function to determine whether the control type is compatible with
542    VIDIOC_G/S_CTRL. */
543 static bool type_is_int(const struct v4l2_ctrl *ctrl)
544 {
545         switch (ctrl->type) {
546         case V4L2_CTRL_TYPE_INTEGER64:
547         case V4L2_CTRL_TYPE_STRING:
548                 /* Nope, these need v4l2_ext_control */
549                 return false;
550         default:
551                 return true;
552         }
553 }
554
555 /* Helper function: copy the current control value back to the caller */
556 static int cur_to_user(struct v4l2_ext_control *c,
557                        struct v4l2_ctrl *ctrl)
558 {
559         u32 len;
560
561         switch (ctrl->type) {
562         case V4L2_CTRL_TYPE_STRING:
563                 len = strlen(ctrl->cur.string);
564                 if (c->size < len + 1) {
565                         c->size = len + 1;
566                         return -ENOSPC;
567                 }
568                 return copy_to_user(c->string, ctrl->cur.string,
569                                                 len + 1) ? -EFAULT : 0;
570         case V4L2_CTRL_TYPE_INTEGER64:
571                 c->value64 = ctrl->cur.val64;
572                 break;
573         default:
574                 c->value = ctrl->cur.val;
575                 break;
576         }
577         return 0;
578 }
579
580 /* Helper function: copy the caller-provider value as the new control value */
581 static int user_to_new(struct v4l2_ext_control *c,
582                        struct v4l2_ctrl *ctrl)
583 {
584         int ret;
585         u32 size;
586
587         ctrl->is_new = 1;
588         switch (ctrl->type) {
589         case V4L2_CTRL_TYPE_INTEGER64:
590                 ctrl->val64 = c->value64;
591                 break;
592         case V4L2_CTRL_TYPE_STRING:
593                 size = c->size;
594                 if (size == 0)
595                         return -ERANGE;
596                 if (size > ctrl->maximum + 1)
597                         size = ctrl->maximum + 1;
598                 ret = copy_from_user(ctrl->string, c->string, size);
599                 if (!ret) {
600                         char last = ctrl->string[size - 1];
601
602                         ctrl->string[size - 1] = 0;
603                         /* If the string was longer than ctrl->maximum,
604                            then return an error. */
605                         if (strlen(ctrl->string) == ctrl->maximum && last)
606                                 return -ERANGE;
607                 }
608                 return ret ? -EFAULT : 0;
609         default:
610                 ctrl->val = c->value;
611                 break;
612         }
613         return 0;
614 }
615
616 /* Helper function: copy the new control value back to the caller */
617 static int new_to_user(struct v4l2_ext_control *c,
618                        struct v4l2_ctrl *ctrl)
619 {
620         u32 len;
621
622         switch (ctrl->type) {
623         case V4L2_CTRL_TYPE_STRING:
624                 len = strlen(ctrl->string);
625                 if (c->size < len + 1) {
626                         c->size = ctrl->maximum + 1;
627                         return -ENOSPC;
628                 }
629                 return copy_to_user(c->string, ctrl->string,
630                                                 len + 1) ? -EFAULT : 0;
631         case V4L2_CTRL_TYPE_INTEGER64:
632                 c->value64 = ctrl->val64;
633                 break;
634         default:
635                 c->value = ctrl->val;
636                 break;
637         }
638         return 0;
639 }
640
641 /* Copy the new value to the current value. */
642 static void new_to_cur(struct v4l2_ctrl *ctrl)
643 {
644         if (ctrl == NULL)
645                 return;
646         switch (ctrl->type) {
647         case V4L2_CTRL_TYPE_STRING:
648                 /* strings are always 0-terminated */
649                 strcpy(ctrl->cur.string, ctrl->string);
650                 break;
651         case V4L2_CTRL_TYPE_INTEGER64:
652                 ctrl->cur.val64 = ctrl->val64;
653                 break;
654         default:
655                 ctrl->cur.val = ctrl->val;
656                 break;
657         }
658 }
659
660 /* Copy the current value to the new value */
661 static void cur_to_new(struct v4l2_ctrl *ctrl)
662 {
663         if (ctrl == NULL)
664                 return;
665         switch (ctrl->type) {
666         case V4L2_CTRL_TYPE_STRING:
667                 /* strings are always 0-terminated */
668                 strcpy(ctrl->string, ctrl->cur.string);
669                 break;
670         case V4L2_CTRL_TYPE_INTEGER64:
671                 ctrl->val64 = ctrl->cur.val64;
672                 break;
673         default:
674                 ctrl->val = ctrl->cur.val;
675                 break;
676         }
677 }
678
679 /* Return non-zero if one or more of the controls in the cluster has a new
680    value that differs from the current value. */
681 static int cluster_changed(struct v4l2_ctrl *master)
682 {
683         int diff = 0;
684         int i;
685
686         for (i = 0; !diff && i < master->ncontrols; i++) {
687                 struct v4l2_ctrl *ctrl = master->cluster[i];
688
689                 if (ctrl == NULL)
690                         continue;
691                 switch (ctrl->type) {
692                 case V4L2_CTRL_TYPE_BUTTON:
693                         /* Button controls are always 'different' */
694                         return 1;
695                 case V4L2_CTRL_TYPE_STRING:
696                         /* strings are always 0-terminated */
697                         diff = strcmp(ctrl->string, ctrl->cur.string);
698                         break;
699                 case V4L2_CTRL_TYPE_INTEGER64:
700                         diff = ctrl->val64 != ctrl->cur.val64;
701                         break;
702                 default:
703                         diff = ctrl->val != ctrl->cur.val;
704                         break;
705                 }
706         }
707         return diff;
708 }
709
710 /* Validate a new control */
711 static int validate_new(struct v4l2_ctrl *ctrl)
712 {
713         s32 val = ctrl->val;
714         char *s = ctrl->string;
715         u32 offset;
716         size_t len;
717
718         switch (ctrl->type) {
719         case V4L2_CTRL_TYPE_INTEGER:
720                 /* Round towards the closest legal value */
721                 val += ctrl->step / 2;
722                 if (val < ctrl->minimum)
723                         val = ctrl->minimum;
724                 if (val > ctrl->maximum)
725                         val = ctrl->maximum;
726                 offset = val - ctrl->minimum;
727                 offset = ctrl->step * (offset / ctrl->step);
728                 val = ctrl->minimum + offset;
729                 ctrl->val = val;
730                 return 0;
731
732         case V4L2_CTRL_TYPE_BOOLEAN:
733                 ctrl->val = !!ctrl->val;
734                 return 0;
735
736         case V4L2_CTRL_TYPE_MENU:
737                 if (val < ctrl->minimum || val > ctrl->maximum)
738                         return -ERANGE;
739                 if (ctrl->qmenu[val][0] == '\0' ||
740                     (ctrl->menu_skip_mask & (1 << val)))
741                         return -EINVAL;
742                 return 0;
743
744         case V4L2_CTRL_TYPE_BUTTON:
745         case V4L2_CTRL_TYPE_CTRL_CLASS:
746                 ctrl->val64 = 0;
747                 return 0;
748
749         case V4L2_CTRL_TYPE_INTEGER64:
750                 return 0;
751
752         case V4L2_CTRL_TYPE_STRING:
753                 len = strlen(s);
754                 if (len < ctrl->minimum)
755                         return -ERANGE;
756                 if ((len - ctrl->minimum) % ctrl->step)
757                         return -ERANGE;
758                 return 0;
759
760         default:
761                 return -EINVAL;
762         }
763 }
764
765 static inline u32 node2id(struct list_head *node)
766 {
767         return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
768 }
769
770 /* Set the handler's error code if it wasn't set earlier already */
771 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
772 {
773         if (hdl->error == 0)
774                 hdl->error = err;
775         return err;
776 }
777
778 /* Initialize the handler */
779 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
780                            unsigned nr_of_controls_hint)
781 {
782         mutex_init(&hdl->lock);
783         INIT_LIST_HEAD(&hdl->ctrls);
784         INIT_LIST_HEAD(&hdl->ctrl_refs);
785         hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
786         hdl->buckets = kzalloc(sizeof(hdl->buckets[0]) * hdl->nr_of_buckets,
787                                                                 GFP_KERNEL);
788         hdl->error = hdl->buckets ? 0 : -ENOMEM;
789         return hdl->error;
790 }
791 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
792
793 /* Free all controls and control refs */
794 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
795 {
796         struct v4l2_ctrl_ref *ref, *next_ref;
797         struct v4l2_ctrl *ctrl, *next_ctrl;
798
799         if (hdl == NULL || hdl->buckets == NULL)
800                 return;
801
802         mutex_lock(&hdl->lock);
803         /* Free all nodes */
804         list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
805                 list_del(&ref->node);
806                 kfree(ref);
807         }
808         /* Free all controls owned by the handler */
809         list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
810                 list_del(&ctrl->node);
811                 kfree(ctrl);
812         }
813         kfree(hdl->buckets);
814         hdl->buckets = NULL;
815         hdl->cached = NULL;
816         hdl->error = 0;
817         mutex_unlock(&hdl->lock);
818 }
819 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
820
821 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
822    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
823    with applications that do not use the NEXT_CTRL flag.
824
825    We just find the n-th private user control. It's O(N), but that should not
826    be an issue in this particular case. */
827 static struct v4l2_ctrl_ref *find_private_ref(
828                 struct v4l2_ctrl_handler *hdl, u32 id)
829 {
830         struct v4l2_ctrl_ref *ref;
831
832         id -= V4L2_CID_PRIVATE_BASE;
833         list_for_each_entry(ref, &hdl->ctrl_refs, node) {
834                 /* Search for private user controls that are compatible with
835                    VIDIOC_G/S_CTRL. */
836                 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
837                     V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
838                         if (!type_is_int(ref->ctrl))
839                                 continue;
840                         if (id == 0)
841                                 return ref;
842                         id--;
843                 }
844         }
845         return NULL;
846 }
847
848 /* Find a control with the given ID. */
849 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
850 {
851         struct v4l2_ctrl_ref *ref;
852         int bucket;
853
854         id &= V4L2_CTRL_ID_MASK;
855
856         /* Old-style private controls need special handling */
857         if (id >= V4L2_CID_PRIVATE_BASE)
858                 return find_private_ref(hdl, id);
859         bucket = id % hdl->nr_of_buckets;
860
861         /* Simple optimization: cache the last control found */
862         if (hdl->cached && hdl->cached->ctrl->id == id)
863                 return hdl->cached;
864
865         /* Not in cache, search the hash */
866         ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
867         while (ref && ref->ctrl->id != id)
868                 ref = ref->next;
869
870         if (ref)
871                 hdl->cached = ref; /* cache it! */
872         return ref;
873 }
874
875 /* Find a control with the given ID. Take the handler's lock first. */
876 static struct v4l2_ctrl_ref *find_ref_lock(
877                 struct v4l2_ctrl_handler *hdl, u32 id)
878 {
879         struct v4l2_ctrl_ref *ref = NULL;
880
881         if (hdl) {
882                 mutex_lock(&hdl->lock);
883                 ref = find_ref(hdl, id);
884                 mutex_unlock(&hdl->lock);
885         }
886         return ref;
887 }
888
889 /* Find a control with the given ID. */
890 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
891 {
892         struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
893
894         return ref ? ref->ctrl : NULL;
895 }
896 EXPORT_SYMBOL(v4l2_ctrl_find);
897
898 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
899 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
900                            struct v4l2_ctrl *ctrl)
901 {
902         struct v4l2_ctrl_ref *ref;
903         struct v4l2_ctrl_ref *new_ref;
904         u32 id = ctrl->id;
905         u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
906         int bucket = id % hdl->nr_of_buckets;   /* which bucket to use */
907
908         /* Automatically add the control class if it is not yet present. */
909         if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
910                 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
911                         return hdl->error;
912
913         if (hdl->error)
914                 return hdl->error;
915
916         new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
917         if (!new_ref)
918                 return handler_set_err(hdl, -ENOMEM);
919         new_ref->ctrl = ctrl;
920         if (ctrl->handler == hdl) {
921                 /* By default each control starts in a cluster of its own.
922                    new_ref->ctrl is basically a cluster array with one
923                    element, so that's perfect to use as the cluster pointer.
924                    But only do this for the handler that owns the control. */
925                 ctrl->cluster = &new_ref->ctrl;
926                 ctrl->ncontrols = 1;
927         }
928
929         INIT_LIST_HEAD(&new_ref->node);
930
931         mutex_lock(&hdl->lock);
932
933         /* Add immediately at the end of the list if the list is empty, or if
934            the last element in the list has a lower ID.
935            This ensures that when elements are added in ascending order the
936            insertion is an O(1) operation. */
937         if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
938                 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
939                 goto insert_in_hash;
940         }
941
942         /* Find insert position in sorted list */
943         list_for_each_entry(ref, &hdl->ctrl_refs, node) {
944                 if (ref->ctrl->id < id)
945                         continue;
946                 /* Don't add duplicates */
947                 if (ref->ctrl->id == id) {
948                         kfree(new_ref);
949                         goto unlock;
950                 }
951                 list_add(&new_ref->node, ref->node.prev);
952                 break;
953         }
954
955 insert_in_hash:
956         /* Insert the control node in the hash */
957         new_ref->next = hdl->buckets[bucket];
958         hdl->buckets[bucket] = new_ref;
959
960 unlock:
961         mutex_unlock(&hdl->lock);
962         return 0;
963 }
964
965 /* Add a new control */
966 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
967                         const struct v4l2_ctrl_ops *ops,
968                         u32 id, const char *name, enum v4l2_ctrl_type type,
969                         s32 min, s32 max, u32 step, s32 def,
970                         u32 flags, const char * const *qmenu, void *priv)
971 {
972         struct v4l2_ctrl *ctrl;
973         unsigned sz_extra = 0;
974
975         if (hdl->error)
976                 return NULL;
977
978         /* Sanity checks */
979         if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
980             max < min ||
981             (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
982             (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
983             (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
984                 handler_set_err(hdl, -ERANGE);
985                 return NULL;
986         }
987         if ((type == V4L2_CTRL_TYPE_INTEGER ||
988              type == V4L2_CTRL_TYPE_MENU ||
989              type == V4L2_CTRL_TYPE_BOOLEAN) &&
990             (def < min || def > max)) {
991                 handler_set_err(hdl, -ERANGE);
992                 return NULL;
993         }
994
995         if (type == V4L2_CTRL_TYPE_BUTTON)
996                 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
997         else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
998                 flags |= V4L2_CTRL_FLAG_READ_ONLY;
999         else if (type == V4L2_CTRL_TYPE_STRING)
1000                 sz_extra += 2 * (max + 1);
1001
1002         ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1003         if (ctrl == NULL) {
1004                 handler_set_err(hdl, -ENOMEM);
1005                 return NULL;
1006         }
1007
1008         INIT_LIST_HEAD(&ctrl->node);
1009         ctrl->handler = hdl;
1010         ctrl->ops = ops;
1011         ctrl->id = id;
1012         ctrl->name = name;
1013         ctrl->type = type;
1014         ctrl->flags = flags;
1015         ctrl->minimum = min;
1016         ctrl->maximum = max;
1017         ctrl->step = step;
1018         ctrl->qmenu = qmenu;
1019         ctrl->priv = priv;
1020         ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1021
1022         if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1023                 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1024                 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1025                 if (ctrl->minimum)
1026                         memset(ctrl->cur.string, ' ', ctrl->minimum);
1027         }
1028         if (handler_new_ref(hdl, ctrl)) {
1029                 kfree(ctrl);
1030                 return NULL;
1031         }
1032         mutex_lock(&hdl->lock);
1033         list_add_tail(&ctrl->node, &hdl->ctrls);
1034         mutex_unlock(&hdl->lock);
1035         return ctrl;
1036 }
1037
1038 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1039                         const struct v4l2_ctrl_config *cfg, void *priv)
1040 {
1041         bool is_menu;
1042         struct v4l2_ctrl *ctrl;
1043         const char *name = cfg->name;
1044         const char * const *qmenu = cfg->qmenu;
1045         enum v4l2_ctrl_type type = cfg->type;
1046         u32 flags = cfg->flags;
1047         s32 min = cfg->min;
1048         s32 max = cfg->max;
1049         u32 step = cfg->step;
1050         s32 def = cfg->def;
1051
1052         if (name == NULL)
1053                 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1054                                                                 &def, &flags);
1055
1056         is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU);
1057         if (is_menu)
1058                 WARN_ON(step);
1059         else
1060                 WARN_ON(cfg->menu_skip_mask);
1061         if (is_menu && qmenu == NULL)
1062                 qmenu = v4l2_ctrl_get_menu(cfg->id);
1063
1064         ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1065                         type, min, max,
1066                         is_menu ? cfg->menu_skip_mask : step,
1067                         def, flags, qmenu, priv);
1068         if (ctrl) {
1069                 ctrl->is_private = cfg->is_private;
1070                 ctrl->is_volatile = cfg->is_volatile;
1071         }
1072         return ctrl;
1073 }
1074 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1075
1076 /* Helper function for standard non-menu controls */
1077 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1078                         const struct v4l2_ctrl_ops *ops,
1079                         u32 id, s32 min, s32 max, u32 step, s32 def)
1080 {
1081         const char *name;
1082         enum v4l2_ctrl_type type;
1083         u32 flags;
1084
1085         v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1086         if (type == V4L2_CTRL_TYPE_MENU) {
1087                 handler_set_err(hdl, -EINVAL);
1088                 return NULL;
1089         }
1090         return v4l2_ctrl_new(hdl, ops, id, name, type,
1091                                     min, max, step, def, flags, NULL, NULL);
1092 }
1093 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1094
1095 /* Helper function for standard menu controls */
1096 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1097                         const struct v4l2_ctrl_ops *ops,
1098                         u32 id, s32 max, s32 mask, s32 def)
1099 {
1100         const char * const *qmenu = v4l2_ctrl_get_menu(id);
1101         const char *name;
1102         enum v4l2_ctrl_type type;
1103         s32 min;
1104         s32 step;
1105         u32 flags;
1106
1107         v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1108         if (type != V4L2_CTRL_TYPE_MENU) {
1109                 handler_set_err(hdl, -EINVAL);
1110                 return NULL;
1111         }
1112         return v4l2_ctrl_new(hdl, ops, id, name, type,
1113                                     0, max, mask, def, flags, qmenu, NULL);
1114 }
1115 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1116
1117 /* Add a control from another handler to this handler */
1118 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1119                                           struct v4l2_ctrl *ctrl)
1120 {
1121         if (hdl == NULL || hdl->error)
1122                 return NULL;
1123         if (ctrl == NULL) {
1124                 handler_set_err(hdl, -EINVAL);
1125                 return NULL;
1126         }
1127         if (ctrl->handler == hdl)
1128                 return ctrl;
1129         return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1130 }
1131 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1132
1133 /* Add the controls from another handler to our own. */
1134 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1135                           struct v4l2_ctrl_handler *add)
1136 {
1137         struct v4l2_ctrl *ctrl;
1138         int ret = 0;
1139
1140         /* Do nothing if either handler is NULL or if they are the same */
1141         if (!hdl || !add || hdl == add)
1142                 return 0;
1143         if (hdl->error)
1144                 return hdl->error;
1145         mutex_lock(&add->lock);
1146         list_for_each_entry(ctrl, &add->ctrls, node) {
1147                 /* Skip handler-private controls. */
1148                 if (ctrl->is_private)
1149                         continue;
1150                 ret = handler_new_ref(hdl, ctrl);
1151                 if (ret)
1152                         break;
1153         }
1154         mutex_unlock(&add->lock);
1155         return ret;
1156 }
1157 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1158
1159 /* Cluster controls */
1160 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1161 {
1162         int i;
1163
1164         /* The first control is the master control and it must not be NULL */
1165         BUG_ON(controls[0] == NULL);
1166
1167         for (i = 0; i < ncontrols; i++) {
1168                 if (controls[i]) {
1169                         controls[i]->cluster = controls;
1170                         controls[i]->ncontrols = ncontrols;
1171                 }
1172         }
1173 }
1174 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1175
1176 /* Activate/deactivate a control. */
1177 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1178 {
1179         if (ctrl == NULL)
1180                 return;
1181
1182         if (!active)
1183                 /* set V4L2_CTRL_FLAG_INACTIVE */
1184                 set_bit(4, &ctrl->flags);
1185         else
1186                 /* clear V4L2_CTRL_FLAG_INACTIVE */
1187                 clear_bit(4, &ctrl->flags);
1188 }
1189 EXPORT_SYMBOL(v4l2_ctrl_activate);
1190
1191 /* Grab/ungrab a control.
1192    Typically used when streaming starts and you want to grab controls,
1193    preventing the user from changing them.
1194
1195    Just call this and the framework will block any attempts to change
1196    these controls. */
1197 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1198 {
1199         if (ctrl == NULL)
1200                 return;
1201
1202         if (grabbed)
1203                 /* set V4L2_CTRL_FLAG_GRABBED */
1204                 set_bit(1, &ctrl->flags);
1205         else
1206                 /* clear V4L2_CTRL_FLAG_GRABBED */
1207                 clear_bit(1, &ctrl->flags);
1208 }
1209 EXPORT_SYMBOL(v4l2_ctrl_grab);
1210
1211 /* Log the control name and value */
1212 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1213                      const char *prefix, const char *colon)
1214 {
1215         int fl_inact = ctrl->flags & V4L2_CTRL_FLAG_INACTIVE;
1216         int fl_grabbed = ctrl->flags & V4L2_CTRL_FLAG_GRABBED;
1217
1218         if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1219                 return;
1220         if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1221                 return;
1222
1223         printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1224
1225         switch (ctrl->type) {
1226         case V4L2_CTRL_TYPE_INTEGER:
1227                 printk(KERN_CONT "%d", ctrl->cur.val);
1228                 break;
1229         case V4L2_CTRL_TYPE_BOOLEAN:
1230                 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1231                 break;
1232         case V4L2_CTRL_TYPE_MENU:
1233                 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1234                 break;
1235         case V4L2_CTRL_TYPE_INTEGER64:
1236                 printk(KERN_CONT "%lld", ctrl->cur.val64);
1237                 break;
1238         case V4L2_CTRL_TYPE_STRING:
1239                 printk(KERN_CONT "%s", ctrl->cur.string);
1240                 break;
1241         default:
1242                 printk(KERN_CONT "unknown type %d", ctrl->type);
1243                 break;
1244         }
1245         if (fl_inact && fl_grabbed)
1246                 printk(KERN_CONT " (inactive, grabbed)\n");
1247         else if (fl_inact)
1248                 printk(KERN_CONT " (inactive)\n");
1249         else if (fl_grabbed)
1250                 printk(KERN_CONT " (grabbed)\n");
1251         else
1252                 printk(KERN_CONT "\n");
1253 }
1254
1255 /* Log all controls owned by the handler */
1256 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1257                                   const char *prefix)
1258 {
1259         struct v4l2_ctrl *ctrl;
1260         const char *colon = "";
1261         int len;
1262
1263         if (hdl == NULL)
1264                 return;
1265         if (prefix == NULL)
1266                 prefix = "";
1267         len = strlen(prefix);
1268         if (len && prefix[len - 1] != ' ')
1269                 colon = ": ";
1270         mutex_lock(&hdl->lock);
1271         list_for_each_entry(ctrl, &hdl->ctrls, node)
1272                 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1273                         log_ctrl(ctrl, prefix, colon);
1274         mutex_unlock(&hdl->lock);
1275 }
1276 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1277
1278 /* Call s_ctrl for all controls owned by the handler */
1279 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1280 {
1281         struct v4l2_ctrl *ctrl;
1282         int ret = 0;
1283
1284         if (hdl == NULL)
1285                 return 0;
1286         mutex_lock(&hdl->lock);
1287         list_for_each_entry(ctrl, &hdl->ctrls, node)
1288                 ctrl->done = false;
1289
1290         list_for_each_entry(ctrl, &hdl->ctrls, node) {
1291                 struct v4l2_ctrl *master = ctrl->cluster[0];
1292                 int i;
1293
1294                 /* Skip if this control was already handled by a cluster. */
1295                 if (ctrl->done)
1296                         continue;
1297
1298                 for (i = 0; i < master->ncontrols; i++) {
1299                         if (master->cluster[i]) {
1300                                 cur_to_new(master->cluster[i]);
1301                                 master->cluster[i]->is_new = 1;
1302                         }
1303                 }
1304
1305                 /* Skip button controls and read-only controls. */
1306                 if (ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1307                     (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1308                         continue;
1309                 ret = master->ops->s_ctrl(master);
1310                 if (ret)
1311                         break;
1312                 for (i = 0; i < master->ncontrols; i++)
1313                         if (master->cluster[i])
1314                                 master->cluster[i]->done = true;
1315         }
1316         mutex_unlock(&hdl->lock);
1317         return ret;
1318 }
1319 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1320
1321 /* Implement VIDIOC_QUERYCTRL */
1322 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1323 {
1324         u32 id = qc->id & V4L2_CTRL_ID_MASK;
1325         struct v4l2_ctrl_ref *ref;
1326         struct v4l2_ctrl *ctrl;
1327
1328         if (hdl == NULL)
1329                 return -EINVAL;
1330
1331         mutex_lock(&hdl->lock);
1332
1333         /* Try to find it */
1334         ref = find_ref(hdl, id);
1335
1336         if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1337                 /* Find the next control with ID > qc->id */
1338
1339                 /* Did we reach the end of the control list? */
1340                 if (id >= node2id(hdl->ctrl_refs.prev)) {
1341                         ref = NULL; /* Yes, so there is no next control */
1342                 } else if (ref) {
1343                         /* We found a control with the given ID, so just get
1344                            the next one in the list. */
1345                         ref = list_entry(ref->node.next, typeof(*ref), node);
1346                 } else {
1347                         /* No control with the given ID exists, so start
1348                            searching for the next largest ID. We know there
1349                            is one, otherwise the first 'if' above would have
1350                            been true. */
1351                         list_for_each_entry(ref, &hdl->ctrl_refs, node)
1352                                 if (id < ref->ctrl->id)
1353                                         break;
1354                 }
1355         }
1356         mutex_unlock(&hdl->lock);
1357         if (!ref)
1358                 return -EINVAL;
1359
1360         ctrl = ref->ctrl;
1361         memset(qc, 0, sizeof(*qc));
1362         if (id >= V4L2_CID_PRIVATE_BASE)
1363                 qc->id = id;
1364         else
1365                 qc->id = ctrl->id;
1366         strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1367         qc->minimum = ctrl->minimum;
1368         qc->maximum = ctrl->maximum;
1369         qc->default_value = ctrl->default_value;
1370         if (ctrl->type == V4L2_CTRL_TYPE_MENU)
1371                 qc->step = 1;
1372         else
1373                 qc->step = ctrl->step;
1374         qc->flags = ctrl->flags;
1375         qc->type = ctrl->type;
1376         return 0;
1377 }
1378 EXPORT_SYMBOL(v4l2_queryctrl);
1379
1380 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1381 {
1382         if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1383                 return -EINVAL;
1384         return v4l2_queryctrl(sd->ctrl_handler, qc);
1385 }
1386 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1387
1388 /* Implement VIDIOC_QUERYMENU */
1389 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1390 {
1391         struct v4l2_ctrl *ctrl;
1392         u32 i = qm->index;
1393
1394         ctrl = v4l2_ctrl_find(hdl, qm->id);
1395         if (!ctrl)
1396                 return -EINVAL;
1397
1398         qm->reserved = 0;
1399         /* Sanity checks */
1400         if (ctrl->qmenu == NULL ||
1401             i < ctrl->minimum || i > ctrl->maximum)
1402                 return -EINVAL;
1403         /* Use mask to see if this menu item should be skipped */
1404         if (ctrl->menu_skip_mask & (1 << i))
1405                 return -EINVAL;
1406         /* Empty menu items should also be skipped */
1407         if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1408                 return -EINVAL;
1409         strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1410         return 0;
1411 }
1412 EXPORT_SYMBOL(v4l2_querymenu);
1413
1414 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
1415 {
1416         return v4l2_querymenu(sd->ctrl_handler, qm);
1417 }
1418 EXPORT_SYMBOL(v4l2_subdev_querymenu);
1419
1420
1421
1422 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
1423
1424    It is not a fully atomic operation, just best-effort only. After all, if
1425    multiple controls have to be set through multiple i2c writes (for example)
1426    then some initial writes may succeed while others fail. Thus leaving the
1427    system in an inconsistent state. The question is how much effort you are
1428    willing to spend on trying to make something atomic that really isn't.
1429
1430    From the point of view of an application the main requirement is that
1431    when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
1432    error should be returned without actually affecting any controls.
1433
1434    If all the values are correct, then it is acceptable to just give up
1435    in case of low-level errors.
1436
1437    It is important though that the application can tell when only a partial
1438    configuration was done. The way we do that is through the error_idx field
1439    of struct v4l2_ext_controls: if that is equal to the count field then no
1440    controls were affected. Otherwise all controls before that index were
1441    successful in performing their 'get' or 'set' operation, the control at
1442    the given index failed, and you don't know what happened with the controls
1443    after the failed one. Since if they were part of a control cluster they
1444    could have been successfully processed (if a cluster member was encountered
1445    at index < error_idx), they could have failed (if a cluster member was at
1446    error_idx), or they may not have been processed yet (if the first cluster
1447    member appeared after error_idx).
1448
1449    It is all fairly theoretical, though. In practice all you can do is to
1450    bail out. If error_idx == count, then it is an application bug. If
1451    error_idx < count then it is only an application bug if the error code was
1452    EBUSY. That usually means that something started streaming just when you
1453    tried to set the controls. In all other cases it is a driver/hardware
1454    problem and all you can do is to retry or bail out.
1455
1456    Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
1457    never modifies controls the error_idx is just set to whatever control
1458    has an invalid value.
1459  */
1460
1461 /* Prepare for the extended g/s/try functions.
1462    Find the controls in the control array and do some basic checks. */
1463 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1464                              struct v4l2_ext_controls *cs,
1465                              struct ctrl_helper *helpers,
1466                              bool try)
1467 {
1468         u32 i;
1469
1470         for (i = 0; i < cs->count; i++) {
1471                 struct v4l2_ext_control *c = &cs->controls[i];
1472                 struct v4l2_ctrl *ctrl;
1473                 u32 id = c->id & V4L2_CTRL_ID_MASK;
1474
1475                 if (try)
1476                         cs->error_idx = i;
1477
1478                 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
1479                         return -EINVAL;
1480
1481                 /* Old-style private controls are not allowed for
1482                    extended controls */
1483                 if (id >= V4L2_CID_PRIVATE_BASE)
1484                         return -EINVAL;
1485                 ctrl = v4l2_ctrl_find(hdl, id);
1486                 if (ctrl == NULL)
1487                         return -EINVAL;
1488                 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
1489                         return -EINVAL;
1490
1491                 helpers[i].ctrl = ctrl;
1492                 helpers[i].handled = false;
1493         }
1494         return 0;
1495 }
1496
1497 typedef int (*cluster_func)(struct v4l2_ext_control *c,
1498                             struct v4l2_ctrl *ctrl);
1499
1500 /* Walk over all controls in v4l2_ext_controls belonging to the same cluster
1501    and call the provided function. */
1502 static int cluster_walk(unsigned from,
1503                         struct v4l2_ext_controls *cs,
1504                         struct ctrl_helper *helpers,
1505                         cluster_func f)
1506 {
1507         struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1508         int ret = 0;
1509         int i;
1510
1511         /* Find any controls from the same cluster and call the function */
1512         for (i = from; !ret && i < cs->count; i++) {
1513                 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1514
1515                 if (!helpers[i].handled && ctrl->cluster == cluster)
1516                         ret = f(&cs->controls[i], ctrl);
1517         }
1518         return ret;
1519 }
1520
1521 static void cluster_done(unsigned from,
1522                          struct v4l2_ext_controls *cs,
1523                          struct ctrl_helper *helpers)
1524 {
1525         struct v4l2_ctrl **cluster = helpers[from].ctrl->cluster;
1526         int i;
1527
1528         /* Find any controls from the same cluster and mark them as handled */
1529         for (i = from; i < cs->count; i++)
1530                 if (helpers[i].ctrl->cluster == cluster)
1531                         helpers[i].handled = true;
1532 }
1533
1534 /* Handles the corner case where cs->count == 0. It checks whether the
1535    specified control class exists. If that class ID is 0, then it checks
1536    whether there are any controls at all. */
1537 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
1538 {
1539         if (ctrl_class == 0)
1540                 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
1541         return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
1542 }
1543
1544
1545
1546 /* Get extended controls. Allocates the helpers array if needed. */
1547 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1548 {
1549         struct ctrl_helper helper[4];
1550         struct ctrl_helper *helpers = helper;
1551         int ret;
1552         int i;
1553
1554         cs->error_idx = cs->count;
1555         cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1556
1557         if (hdl == NULL)
1558                 return -EINVAL;
1559
1560         if (cs->count == 0)
1561                 return class_check(hdl, cs->ctrl_class);
1562
1563         if (cs->count > ARRAY_SIZE(helper)) {
1564                 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1565                 if (helpers == NULL)
1566                         return -ENOMEM;
1567         }
1568
1569         ret = prepare_ext_ctrls(hdl, cs, helpers, false);
1570
1571         for (i = 0; !ret && i < cs->count; i++)
1572                 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1573                         ret = -EACCES;
1574
1575         for (i = 0; !ret && i < cs->count; i++) {
1576                 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1577                 struct v4l2_ctrl *master = ctrl->cluster[0];
1578
1579                 if (helpers[i].handled)
1580                         continue;
1581
1582                 cs->error_idx = i;
1583
1584                 v4l2_ctrl_lock(master);
1585                 /* g_volatile_ctrl will update the current control values */
1586                 if (ctrl->is_volatile && master->ops->g_volatile_ctrl)
1587                         ret = master->ops->g_volatile_ctrl(master);
1588                 /* If OK, then copy the current control values to the caller */
1589                 if (!ret)
1590                         ret = cluster_walk(i, cs, helpers, cur_to_user);
1591                 v4l2_ctrl_unlock(master);
1592                 cluster_done(i, cs, helpers);
1593         }
1594
1595         if (cs->count > ARRAY_SIZE(helper))
1596                 kfree(helpers);
1597         return ret;
1598 }
1599 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
1600
1601 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1602 {
1603         return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
1604 }
1605 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
1606
1607 /* Helper function to get a single control */
1608 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1609 {
1610         struct v4l2_ctrl *master = ctrl->cluster[0];
1611         int ret = 0;
1612
1613         if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
1614                 return -EACCES;
1615
1616         v4l2_ctrl_lock(master);
1617         /* g_volatile_ctrl will update the current control values */
1618         if (ctrl->is_volatile && master->ops->g_volatile_ctrl)
1619                 ret = master->ops->g_volatile_ctrl(master);
1620         *val = ctrl->cur.val;
1621         v4l2_ctrl_unlock(master);
1622         return ret;
1623 }
1624
1625 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1626 {
1627         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1628
1629         if (ctrl == NULL || !type_is_int(ctrl))
1630                 return -EINVAL;
1631         return get_ctrl(ctrl, &control->value);
1632 }
1633 EXPORT_SYMBOL(v4l2_g_ctrl);
1634
1635 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1636 {
1637         return v4l2_g_ctrl(sd->ctrl_handler, control);
1638 }
1639 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
1640
1641 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
1642 {
1643         s32 val = 0;
1644
1645         /* It's a driver bug if this happens. */
1646         WARN_ON(!type_is_int(ctrl));
1647         get_ctrl(ctrl, &val);
1648         return val;
1649 }
1650 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
1651
1652
1653 /* Core function that calls try/s_ctrl and ensures that the new value is
1654    copied to the current value on a set.
1655    Must be called with ctrl->handler->lock held. */
1656 static int try_or_set_control_cluster(struct v4l2_ctrl *master, bool set)
1657 {
1658         bool try = !set;
1659         int ret = 0;
1660         int i;
1661
1662         /* Go through the cluster and either validate the new value or
1663            (if no new value was set), copy the current value to the new
1664            value, ensuring a consistent view for the control ops when
1665            called. */
1666         for (i = 0; !ret && i < master->ncontrols; i++) {
1667                 struct v4l2_ctrl *ctrl = master->cluster[i];
1668
1669                 if (ctrl == NULL)
1670                         continue;
1671
1672                 if (ctrl->is_new) {
1673                         /* Double check this: it may have changed since the
1674                            last check in try_or_set_ext_ctrls(). */
1675                         if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1676                                 return -EBUSY;
1677
1678                         /* Validate if required */
1679                         if (!set)
1680                                 ret = validate_new(ctrl);
1681                         continue;
1682                 }
1683                 /* No new value was set, so copy the current and force
1684                    a call to try_ctrl later, since the values for the cluster
1685                    may now have changed and the end result might be invalid. */
1686                 try = true;
1687                 cur_to_new(ctrl);
1688         }
1689
1690         /* For larger clusters you have to call try_ctrl again to
1691            verify that the controls are still valid after the
1692            'cur_to_new' above. */
1693         if (!ret && master->ops->try_ctrl && try)
1694                 ret = master->ops->try_ctrl(master);
1695
1696         /* Don't set if there is no change */
1697         if (!ret && set && cluster_changed(master)) {
1698                 ret = master->ops->s_ctrl(master);
1699                 /* If OK, then make the new values permanent. */
1700                 if (!ret)
1701                         for (i = 0; i < master->ncontrols; i++)
1702                                 new_to_cur(master->cluster[i]);
1703         }
1704         return ret;
1705 }
1706
1707 /* Try or set controls. */
1708 static int try_or_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1709                                 struct v4l2_ext_controls *cs,
1710                                 struct ctrl_helper *helpers,
1711                                 bool set)
1712 {
1713         unsigned i, j;
1714         int ret = 0;
1715
1716         cs->error_idx = cs->count;
1717         for (i = 0; i < cs->count; i++) {
1718                 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1719
1720                 if (!set)
1721                         cs->error_idx = i;
1722
1723                 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1724                         return -EACCES;
1725                 /* This test is also done in try_set_control_cluster() which
1726                    is called in atomic context, so that has the final say,
1727                    but it makes sense to do an up-front check as well. Once
1728                    an error occurs in try_set_control_cluster() some other
1729                    controls may have been set already and we want to do a
1730                    best-effort to avoid that. */
1731                 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
1732                         return -EBUSY;
1733         }
1734
1735         for (i = 0; !ret && i < cs->count; i++) {
1736                 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
1737                 struct v4l2_ctrl *master = ctrl->cluster[0];
1738
1739                 cs->error_idx = i;
1740
1741                 if (helpers[i].handled)
1742                         continue;
1743
1744                 v4l2_ctrl_lock(ctrl);
1745
1746                 /* Reset the 'is_new' flags of the cluster */
1747                 for (j = 0; j < master->ncontrols; j++)
1748                         if (master->cluster[j])
1749                                 master->cluster[j]->is_new = 0;
1750
1751                 /* Copy the new caller-supplied control values.
1752                    user_to_new() sets 'is_new' to 1. */
1753                 ret = cluster_walk(i, cs, helpers, user_to_new);
1754
1755                 if (!ret)
1756                         ret = try_or_set_control_cluster(master, set);
1757
1758                 /* Copy the new values back to userspace. */
1759                 if (!ret)
1760                         ret = cluster_walk(i, cs, helpers, new_to_user);
1761
1762                 v4l2_ctrl_unlock(ctrl);
1763                 cluster_done(i, cs, helpers);
1764         }
1765         return ret;
1766 }
1767
1768 /* Try or try-and-set controls */
1769 static int try_set_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1770                              struct v4l2_ext_controls *cs,
1771                              bool set)
1772 {
1773         struct ctrl_helper helper[4];
1774         struct ctrl_helper *helpers = helper;
1775         int ret;
1776         int i;
1777
1778         cs->error_idx = cs->count;
1779         cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
1780
1781         if (hdl == NULL)
1782                 return -EINVAL;
1783
1784         if (cs->count == 0)
1785                 return class_check(hdl, cs->ctrl_class);
1786
1787         if (cs->count > ARRAY_SIZE(helper)) {
1788                 helpers = kmalloc(sizeof(helper[0]) * cs->count, GFP_KERNEL);
1789                 if (!helpers)
1790                         return -ENOMEM;
1791         }
1792         ret = prepare_ext_ctrls(hdl, cs, helpers, !set);
1793         if (ret)
1794                 goto free;
1795
1796         /* First 'try' all controls and abort on error */
1797         ret = try_or_set_ext_ctrls(hdl, cs, helpers, false);
1798         /* If this is a 'set' operation and the initial 'try' failed,
1799            then set error_idx to count to tell the application that no
1800            controls changed value yet. */
1801         if (set)
1802                 cs->error_idx = cs->count;
1803         if (!ret && set) {
1804                 /* Reset 'handled' state */
1805                 for (i = 0; i < cs->count; i++)
1806                         helpers[i].handled = false;
1807                 ret = try_or_set_ext_ctrls(hdl, cs, helpers, true);
1808         }
1809
1810 free:
1811         if (cs->count > ARRAY_SIZE(helper))
1812                 kfree(helpers);
1813         return ret;
1814 }
1815
1816 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1817 {
1818         return try_set_ext_ctrls(hdl, cs, false);
1819 }
1820 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
1821
1822 int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
1823 {
1824         return try_set_ext_ctrls(hdl, cs, true);
1825 }
1826 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
1827
1828 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1829 {
1830         return try_set_ext_ctrls(sd->ctrl_handler, cs, false);
1831 }
1832 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
1833
1834 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
1835 {
1836         return try_set_ext_ctrls(sd->ctrl_handler, cs, true);
1837 }
1838 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
1839
1840 /* Helper function for VIDIOC_S_CTRL compatibility */
1841 static int set_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
1842 {
1843         struct v4l2_ctrl *master = ctrl->cluster[0];
1844         int ret;
1845         int i;
1846
1847         if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
1848                 return -EACCES;
1849
1850         v4l2_ctrl_lock(ctrl);
1851
1852         /* Reset the 'is_new' flags of the cluster */
1853         for (i = 0; i < master->ncontrols; i++)
1854                 if (master->cluster[i])
1855                         master->cluster[i]->is_new = 0;
1856
1857         ctrl->val = *val;
1858         ctrl->is_new = 1;
1859         ret = try_or_set_control_cluster(master, false);
1860         if (!ret)
1861                 ret = try_or_set_control_cluster(master, true);
1862         *val = ctrl->cur.val;
1863         v4l2_ctrl_unlock(ctrl);
1864         return ret;
1865 }
1866
1867 int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
1868 {
1869         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
1870
1871         if (ctrl == NULL || !type_is_int(ctrl))
1872                 return -EINVAL;
1873
1874         return set_ctrl(ctrl, &control->value);
1875 }
1876 EXPORT_SYMBOL(v4l2_s_ctrl);
1877
1878 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
1879 {
1880         return v4l2_s_ctrl(sd->ctrl_handler, control);
1881 }
1882 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
1883
1884 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1885 {
1886         /* It's a driver bug if this happens. */
1887         WARN_ON(!type_is_int(ctrl));
1888         return set_ctrl(ctrl, &val);
1889 }
1890 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);