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