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