[media] V4L: Add camera 3A lock control
[firefly-linux-kernel-4.4.55.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 <linux/export.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-dev.h>
29
30 #define has_op(master, op) \
31         (master->ops && master->ops->op)
32 #define call_op(master, op) \
33         (has_op(master, op) ? master->ops->op(master) : 0)
34
35 /* Internal temporary helper struct, one for each v4l2_ext_control */
36 struct v4l2_ctrl_helper {
37         /* Pointer to the control reference of the master control */
38         struct v4l2_ctrl_ref *mref;
39         /* The control corresponding to the v4l2_ext_control ID field. */
40         struct v4l2_ctrl *ctrl;
41         /* v4l2_ext_control index of the next control belonging to the
42            same cluster, or 0 if there isn't any. */
43         u32 next;
44 };
45
46 /* Small helper function to determine if the autocluster is set to manual
47    mode. */
48 static bool is_cur_manual(const struct v4l2_ctrl *master)
49 {
50         return master->is_auto && master->cur.val == master->manual_mode_value;
51 }
52
53 /* Same as above, but this checks the against the new value instead of the
54    current value. */
55 static bool is_new_manual(const struct v4l2_ctrl *master)
56 {
57         return master->is_auto && master->val == master->manual_mode_value;
58 }
59
60 /* Returns NULL or a character pointer array containing the menu for
61    the given control ID. The pointer array ends with a NULL pointer.
62    An empty string signifies a menu entry that is invalid. This allows
63    drivers to disable certain options if it is not supported. */
64 const char * const *v4l2_ctrl_get_menu(u32 id)
65 {
66         static const char * const mpeg_audio_sampling_freq[] = {
67                 "44.1 kHz",
68                 "48 kHz",
69                 "32 kHz",
70                 NULL
71         };
72         static const char * const mpeg_audio_encoding[] = {
73                 "MPEG-1/2 Layer I",
74                 "MPEG-1/2 Layer II",
75                 "MPEG-1/2 Layer III",
76                 "MPEG-2/4 AAC",
77                 "AC-3",
78                 NULL
79         };
80         static const char * const mpeg_audio_l1_bitrate[] = {
81                 "32 kbps",
82                 "64 kbps",
83                 "96 kbps",
84                 "128 kbps",
85                 "160 kbps",
86                 "192 kbps",
87                 "224 kbps",
88                 "256 kbps",
89                 "288 kbps",
90                 "320 kbps",
91                 "352 kbps",
92                 "384 kbps",
93                 "416 kbps",
94                 "448 kbps",
95                 NULL
96         };
97         static const char * const mpeg_audio_l2_bitrate[] = {
98                 "32 kbps",
99                 "48 kbps",
100                 "56 kbps",
101                 "64 kbps",
102                 "80 kbps",
103                 "96 kbps",
104                 "112 kbps",
105                 "128 kbps",
106                 "160 kbps",
107                 "192 kbps",
108                 "224 kbps",
109                 "256 kbps",
110                 "320 kbps",
111                 "384 kbps",
112                 NULL
113         };
114         static const char * const mpeg_audio_l3_bitrate[] = {
115                 "32 kbps",
116                 "40 kbps",
117                 "48 kbps",
118                 "56 kbps",
119                 "64 kbps",
120                 "80 kbps",
121                 "96 kbps",
122                 "112 kbps",
123                 "128 kbps",
124                 "160 kbps",
125                 "192 kbps",
126                 "224 kbps",
127                 "256 kbps",
128                 "320 kbps",
129                 NULL
130         };
131         static const char * const mpeg_audio_ac3_bitrate[] = {
132                 "32 kbps",
133                 "40 kbps",
134                 "48 kbps",
135                 "56 kbps",
136                 "64 kbps",
137                 "80 kbps",
138                 "96 kbps",
139                 "112 kbps",
140                 "128 kbps",
141                 "160 kbps",
142                 "192 kbps",
143                 "224 kbps",
144                 "256 kbps",
145                 "320 kbps",
146                 "384 kbps",
147                 "448 kbps",
148                 "512 kbps",
149                 "576 kbps",
150                 "640 kbps",
151                 NULL
152         };
153         static const char * const mpeg_audio_mode[] = {
154                 "Stereo",
155                 "Joint Stereo",
156                 "Dual",
157                 "Mono",
158                 NULL
159         };
160         static const char * const mpeg_audio_mode_extension[] = {
161                 "Bound 4",
162                 "Bound 8",
163                 "Bound 12",
164                 "Bound 16",
165                 NULL
166         };
167         static const char * const mpeg_audio_emphasis[] = {
168                 "No Emphasis",
169                 "50/15 us",
170                 "CCITT J17",
171                 NULL
172         };
173         static const char * const mpeg_audio_crc[] = {
174                 "No CRC",
175                 "16-bit CRC",
176                 NULL
177         };
178         static const char * const mpeg_audio_dec_playback[] = {
179                 "Auto",
180                 "Stereo",
181                 "Left",
182                 "Right",
183                 "Mono",
184                 "Swapped Stereo",
185                 NULL
186         };
187         static const char * const mpeg_video_encoding[] = {
188                 "MPEG-1",
189                 "MPEG-2",
190                 "MPEG-4 AVC",
191                 NULL
192         };
193         static const char * const mpeg_video_aspect[] = {
194                 "1x1",
195                 "4x3",
196                 "16x9",
197                 "2.21x1",
198                 NULL
199         };
200         static const char * const mpeg_video_bitrate_mode[] = {
201                 "Variable Bitrate",
202                 "Constant Bitrate",
203                 NULL
204         };
205         static const char * const mpeg_stream_type[] = {
206                 "MPEG-2 Program Stream",
207                 "MPEG-2 Transport Stream",
208                 "MPEG-1 System Stream",
209                 "MPEG-2 DVD-compatible Stream",
210                 "MPEG-1 VCD-compatible Stream",
211                 "MPEG-2 SVCD-compatible Stream",
212                 NULL
213         };
214         static const char * const mpeg_stream_vbi_fmt[] = {
215                 "No VBI",
216                 "Private Packet, IVTV Format",
217                 NULL
218         };
219         static const char * const camera_power_line_frequency[] = {
220                 "Disabled",
221                 "50 Hz",
222                 "60 Hz",
223                 "Auto",
224                 NULL
225         };
226         static const char * const camera_exposure_auto[] = {
227                 "Auto Mode",
228                 "Manual Mode",
229                 "Shutter Priority Mode",
230                 "Aperture Priority Mode",
231                 NULL
232         };
233         static const char * const camera_exposure_metering[] = {
234                 "Average",
235                 "Center Weighted",
236                 "Spot",
237                 NULL
238         };
239         static const char * const colorfx[] = {
240                 "None",
241                 "Black & White",
242                 "Sepia",
243                 "Negative",
244                 "Emboss",
245                 "Sketch",
246                 "Sky Blue",
247                 "Grass Green",
248                 "Skin Whiten",
249                 "Vivid",
250                 "Aqua",
251                 "Art Freeze",
252                 "Silhouette",
253                 "Solarization",
254                 "Antique",
255                 "Set Cb/Cr",
256                 NULL
257         };
258         static const char * const auto_n_preset_white_balance[] = {
259                 "Manual",
260                 "Auto",
261                 "Incandescent",
262                 "Fluorescent",
263                 "Fluorescent H",
264                 "Horizon",
265                 "Daylight",
266                 "Flash",
267                 "Cloudy",
268                 "Shade",
269                 NULL,
270         };
271         static const char * const camera_iso_sensitivity_auto[] = {
272                 "Manual",
273                 "Auto",
274                 NULL
275         };
276         static const char * const scene_mode[] = {
277                 "None",
278                 "Backlight",
279                 "Beach/Snow",
280                 "Candle Light",
281                 "Dusk/Dawn",
282                 "Fall Colors",
283                 "Fireworks",
284                 "Landscape",
285                 "Night",
286                 "Party/Indoor",
287                 "Portrait",
288                 "Sports",
289                 "Sunset",
290                 "Text",
291                 NULL
292         };
293         static const char * const tune_preemphasis[] = {
294                 "No Preemphasis",
295                 "50 Microseconds",
296                 "75 Microseconds",
297                 NULL,
298         };
299         static const char * const header_mode[] = {
300                 "Separate Buffer",
301                 "Joined With 1st Frame",
302                 NULL,
303         };
304         static const char * const multi_slice[] = {
305                 "Single",
306                 "Max Macroblocks",
307                 "Max Bytes",
308                 NULL,
309         };
310         static const char * const entropy_mode[] = {
311                 "CAVLC",
312                 "CABAC",
313                 NULL,
314         };
315         static const char * const mpeg_h264_level[] = {
316                 "1",
317                 "1b",
318                 "1.1",
319                 "1.2",
320                 "1.3",
321                 "2",
322                 "2.1",
323                 "2.2",
324                 "3",
325                 "3.1",
326                 "3.2",
327                 "4",
328                 "4.1",
329                 "4.2",
330                 "5",
331                 "5.1",
332                 NULL,
333         };
334         static const char * const h264_loop_filter[] = {
335                 "Enabled",
336                 "Disabled",
337                 "Disabled at Slice Boundary",
338                 NULL,
339         };
340         static const char * const h264_profile[] = {
341                 "Baseline",
342                 "Constrained Baseline",
343                 "Main",
344                 "Extended",
345                 "High",
346                 "High 10",
347                 "High 422",
348                 "High 444 Predictive",
349                 "High 10 Intra",
350                 "High 422 Intra",
351                 "High 444 Intra",
352                 "CAVLC 444 Intra",
353                 "Scalable Baseline",
354                 "Scalable High",
355                 "Scalable High Intra",
356                 "Multiview High",
357                 NULL,
358         };
359         static const char * const vui_sar_idc[] = {
360                 "Unspecified",
361                 "1:1",
362                 "12:11",
363                 "10:11",
364                 "16:11",
365                 "40:33",
366                 "24:11",
367                 "20:11",
368                 "32:11",
369                 "80:33",
370                 "18:11",
371                 "15:11",
372                 "64:33",
373                 "160:99",
374                 "4:3",
375                 "3:2",
376                 "2:1",
377                 "Extended SAR",
378                 NULL,
379         };
380         static const char * const mpeg_mpeg4_level[] = {
381                 "0",
382                 "0b",
383                 "1",
384                 "2",
385                 "3",
386                 "3b",
387                 "4",
388                 "5",
389                 NULL,
390         };
391         static const char * const mpeg4_profile[] = {
392                 "Simple",
393                 "Advanced Simple",
394                 "Core",
395                 "Simple Scalable",
396                 "Advanced Coding Efficency",
397                 NULL,
398         };
399
400         static const char * const flash_led_mode[] = {
401                 "Off",
402                 "Flash",
403                 "Torch",
404                 NULL,
405         };
406         static const char * const flash_strobe_source[] = {
407                 "Software",
408                 "External",
409                 NULL,
410         };
411
412         static const char * const jpeg_chroma_subsampling[] = {
413                 "4:4:4",
414                 "4:2:2",
415                 "4:2:0",
416                 "4:1:1",
417                 "4:1:0",
418                 "Gray",
419                 NULL,
420         };
421
422         switch (id) {
423         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
424                 return mpeg_audio_sampling_freq;
425         case V4L2_CID_MPEG_AUDIO_ENCODING:
426                 return mpeg_audio_encoding;
427         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
428                 return mpeg_audio_l1_bitrate;
429         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
430                 return mpeg_audio_l2_bitrate;
431         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
432                 return mpeg_audio_l3_bitrate;
433         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
434                 return mpeg_audio_ac3_bitrate;
435         case V4L2_CID_MPEG_AUDIO_MODE:
436                 return mpeg_audio_mode;
437         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
438                 return mpeg_audio_mode_extension;
439         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
440                 return mpeg_audio_emphasis;
441         case V4L2_CID_MPEG_AUDIO_CRC:
442                 return mpeg_audio_crc;
443         case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
444         case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
445                 return mpeg_audio_dec_playback;
446         case V4L2_CID_MPEG_VIDEO_ENCODING:
447                 return mpeg_video_encoding;
448         case V4L2_CID_MPEG_VIDEO_ASPECT:
449                 return mpeg_video_aspect;
450         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
451                 return mpeg_video_bitrate_mode;
452         case V4L2_CID_MPEG_STREAM_TYPE:
453                 return mpeg_stream_type;
454         case V4L2_CID_MPEG_STREAM_VBI_FMT:
455                 return mpeg_stream_vbi_fmt;
456         case V4L2_CID_POWER_LINE_FREQUENCY:
457                 return camera_power_line_frequency;
458         case V4L2_CID_EXPOSURE_AUTO:
459                 return camera_exposure_auto;
460         case V4L2_CID_EXPOSURE_METERING:
461                 return camera_exposure_metering;
462         case V4L2_CID_COLORFX:
463                 return colorfx;
464         case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
465                 return auto_n_preset_white_balance;
466         case V4L2_CID_ISO_SENSITIVITY_AUTO:
467                 return camera_iso_sensitivity_auto;
468         case V4L2_CID_SCENE_MODE:
469                 return scene_mode;
470         case V4L2_CID_TUNE_PREEMPHASIS:
471                 return tune_preemphasis;
472         case V4L2_CID_FLASH_LED_MODE:
473                 return flash_led_mode;
474         case V4L2_CID_FLASH_STROBE_SOURCE:
475                 return flash_strobe_source;
476         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
477                 return header_mode;
478         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
479                 return multi_slice;
480         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
481                 return entropy_mode;
482         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
483                 return mpeg_h264_level;
484         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
485                 return h264_loop_filter;
486         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
487                 return h264_profile;
488         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
489                 return vui_sar_idc;
490         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
491                 return mpeg_mpeg4_level;
492         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
493                 return mpeg4_profile;
494         case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
495                 return jpeg_chroma_subsampling;
496
497         default:
498                 return NULL;
499         }
500 }
501 EXPORT_SYMBOL(v4l2_ctrl_get_menu);
502
503 /* Return the control name. */
504 const char *v4l2_ctrl_get_name(u32 id)
505 {
506         switch (id) {
507         /* USER controls */
508         /* Keep the order of the 'case's the same as in videodev2.h! */
509         case V4L2_CID_USER_CLASS:               return "User Controls";
510         case V4L2_CID_BRIGHTNESS:               return "Brightness";
511         case V4L2_CID_CONTRAST:                 return "Contrast";
512         case V4L2_CID_SATURATION:               return "Saturation";
513         case V4L2_CID_HUE:                      return "Hue";
514         case V4L2_CID_AUDIO_VOLUME:             return "Volume";
515         case V4L2_CID_AUDIO_BALANCE:            return "Balance";
516         case V4L2_CID_AUDIO_BASS:               return "Bass";
517         case V4L2_CID_AUDIO_TREBLE:             return "Treble";
518         case V4L2_CID_AUDIO_MUTE:               return "Mute";
519         case V4L2_CID_AUDIO_LOUDNESS:           return "Loudness";
520         case V4L2_CID_BLACK_LEVEL:              return "Black Level";
521         case V4L2_CID_AUTO_WHITE_BALANCE:       return "White Balance, Automatic";
522         case V4L2_CID_DO_WHITE_BALANCE:         return "Do White Balance";
523         case V4L2_CID_RED_BALANCE:              return "Red Balance";
524         case V4L2_CID_BLUE_BALANCE:             return "Blue Balance";
525         case V4L2_CID_GAMMA:                    return "Gamma";
526         case V4L2_CID_EXPOSURE:                 return "Exposure";
527         case V4L2_CID_AUTOGAIN:                 return "Gain, Automatic";
528         case V4L2_CID_GAIN:                     return "Gain";
529         case V4L2_CID_HFLIP:                    return "Horizontal Flip";
530         case V4L2_CID_VFLIP:                    return "Vertical Flip";
531         case V4L2_CID_HCENTER:                  return "Horizontal Center";
532         case V4L2_CID_VCENTER:                  return "Vertical Center";
533         case V4L2_CID_POWER_LINE_FREQUENCY:     return "Power Line Frequency";
534         case V4L2_CID_HUE_AUTO:                 return "Hue, Automatic";
535         case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
536         case V4L2_CID_SHARPNESS:                return "Sharpness";
537         case V4L2_CID_BACKLIGHT_COMPENSATION:   return "Backlight Compensation";
538         case V4L2_CID_CHROMA_AGC:               return "Chroma AGC";
539         case V4L2_CID_COLOR_KILLER:             return "Color Killer";
540         case V4L2_CID_COLORFX:                  return "Color Effects";
541         case V4L2_CID_AUTOBRIGHTNESS:           return "Brightness, Automatic";
542         case V4L2_CID_BAND_STOP_FILTER:         return "Band-Stop Filter";
543         case V4L2_CID_ROTATE:                   return "Rotate";
544         case V4L2_CID_BG_COLOR:                 return "Background Color";
545         case V4L2_CID_CHROMA_GAIN:              return "Chroma Gain";
546         case V4L2_CID_ILLUMINATORS_1:           return "Illuminator 1";
547         case V4L2_CID_ILLUMINATORS_2:           return "Illuminator 2";
548         case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:  return "Min Number of Capture Buffers";
549         case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:   return "Min Number of Output Buffers";
550         case V4L2_CID_ALPHA_COMPONENT:          return "Alpha Component";
551         case V4L2_CID_COLORFX_CBCR:             return "Color Effects, CbCr";
552
553         /* MPEG controls */
554         /* Keep the order of the 'case's the same as in videodev2.h! */
555         case V4L2_CID_MPEG_CLASS:               return "MPEG Encoder Controls";
556         case V4L2_CID_MPEG_STREAM_TYPE:         return "Stream Type";
557         case V4L2_CID_MPEG_STREAM_PID_PMT:      return "Stream PMT Program ID";
558         case V4L2_CID_MPEG_STREAM_PID_AUDIO:    return "Stream Audio Program ID";
559         case V4L2_CID_MPEG_STREAM_PID_VIDEO:    return "Stream Video Program ID";
560         case V4L2_CID_MPEG_STREAM_PID_PCR:      return "Stream PCR Program ID";
561         case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
562         case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
563         case V4L2_CID_MPEG_STREAM_VBI_FMT:      return "Stream VBI Format";
564         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
565         case V4L2_CID_MPEG_AUDIO_ENCODING:      return "Audio Encoding";
566         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:    return "Audio Layer I Bitrate";
567         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:    return "Audio Layer II Bitrate";
568         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:    return "Audio Layer III Bitrate";
569         case V4L2_CID_MPEG_AUDIO_MODE:          return "Audio Stereo Mode";
570         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
571         case V4L2_CID_MPEG_AUDIO_EMPHASIS:      return "Audio Emphasis";
572         case V4L2_CID_MPEG_AUDIO_CRC:           return "Audio CRC";
573         case V4L2_CID_MPEG_AUDIO_MUTE:          return "Audio Mute";
574         case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:   return "Audio AAC Bitrate";
575         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:   return "Audio AC-3 Bitrate";
576         case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:  return "Audio Playback";
577         case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
578         case V4L2_CID_MPEG_VIDEO_ENCODING:      return "Video Encoding";
579         case V4L2_CID_MPEG_VIDEO_ASPECT:        return "Video Aspect";
580         case V4L2_CID_MPEG_VIDEO_B_FRAMES:      return "Video B Frames";
581         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:      return "Video GOP Size";
582         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:   return "Video GOP Closure";
583         case V4L2_CID_MPEG_VIDEO_PULLDOWN:      return "Video Pulldown";
584         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:  return "Video Bitrate Mode";
585         case V4L2_CID_MPEG_VIDEO_BITRATE:       return "Video Bitrate";
586         case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:  return "Video Peak Bitrate";
587         case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
588         case V4L2_CID_MPEG_VIDEO_MUTE:          return "Video Mute";
589         case V4L2_CID_MPEG_VIDEO_MUTE_YUV:      return "Video Mute YUV";
590         case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:       return "Decoder Slice Interface";
591         case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:  return "MPEG4 Loop Filter Enable";
592         case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:       return "Number of Intra Refresh MBs";
593         case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:               return "Frame Level Rate Control Enable";
594         case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:                  return "H264 MB Level Rate Control";
595         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:                   return "Sequence Header Mode";
596         case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC:                   return "Max Number of Reference Pics";
597         case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP:               return "H263 I-Frame QP Value";
598         case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP:               return "H263 P-Frame QP Value";
599         case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP:               return "H263 B-Frame QP Value";
600         case V4L2_CID_MPEG_VIDEO_H263_MIN_QP:                   return "H263 Minimum QP Value";
601         case V4L2_CID_MPEG_VIDEO_H263_MAX_QP:                   return "H263 Maximum QP Value";
602         case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:               return "H264 I-Frame QP Value";
603         case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:               return "H264 P-Frame QP Value";
604         case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:               return "H264 B-Frame QP Value";
605         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:                   return "H264 Maximum QP Value";
606         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:                   return "H264 Minimum QP Value";
607         case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:            return "H264 8x8 Transform Enable";
608         case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:                 return "H264 CPB Buffer Size";
609         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:             return "H264 Entropy Mode";
610         case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:                 return "H264 I-Frame Period";
611         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:                    return "H264 Level";
612         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:        return "H264 Loop Filter Alpha Offset";
613         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:         return "H264 Loop Filter Beta Offset";
614         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:         return "H264 Loop Filter Mode";
615         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:                  return "H264 Profile";
616         case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:       return "Vertical Size of SAR";
617         case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:        return "Horizontal Size of SAR";
618         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:           return "Aspect Ratio VUI Enable";
619         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:              return "VUI Aspect Ratio IDC";
620         case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:              return "MPEG4 I-Frame QP Value";
621         case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:              return "MPEG4 P-Frame QP Value";
622         case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP:              return "MPEG4 B-Frame QP Value";
623         case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP:                  return "MPEG4 Minimum QP Value";
624         case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP:                  return "MPEG4 Maximum QP Value";
625         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:                   return "MPEG4 Level";
626         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:                 return "MPEG4 Profile";
627         case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:                    return "Quarter Pixel Search Enable";
628         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:         return "Maximum Bytes in a Slice";
629         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:            return "Number of MBs in a Slice";
630         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:              return "Slice Partitioning Method";
631         case V4L2_CID_MPEG_VIDEO_VBV_SIZE:                      return "VBV Buffer Size";
632         case V4L2_CID_MPEG_VIDEO_DEC_PTS:                       return "Video Decoder PTS";
633         case V4L2_CID_MPEG_VIDEO_DEC_FRAME:                     return "Video Decoder Frame Count";
634
635         /* CAMERA controls */
636         /* Keep the order of the 'case's the same as in videodev2.h! */
637         case V4L2_CID_CAMERA_CLASS:             return "Camera Controls";
638         case V4L2_CID_EXPOSURE_AUTO:            return "Auto Exposure";
639         case V4L2_CID_EXPOSURE_ABSOLUTE:        return "Exposure Time, Absolute";
640         case V4L2_CID_EXPOSURE_AUTO_PRIORITY:   return "Exposure, Dynamic Framerate";
641         case V4L2_CID_PAN_RELATIVE:             return "Pan, Relative";
642         case V4L2_CID_TILT_RELATIVE:            return "Tilt, Relative";
643         case V4L2_CID_PAN_RESET:                return "Pan, Reset";
644         case V4L2_CID_TILT_RESET:               return "Tilt, Reset";
645         case V4L2_CID_PAN_ABSOLUTE:             return "Pan, Absolute";
646         case V4L2_CID_TILT_ABSOLUTE:            return "Tilt, Absolute";
647         case V4L2_CID_FOCUS_ABSOLUTE:           return "Focus, Absolute";
648         case V4L2_CID_FOCUS_RELATIVE:           return "Focus, Relative";
649         case V4L2_CID_FOCUS_AUTO:               return "Focus, Automatic";
650         case V4L2_CID_ZOOM_ABSOLUTE:            return "Zoom, Absolute";
651         case V4L2_CID_ZOOM_RELATIVE:            return "Zoom, Relative";
652         case V4L2_CID_ZOOM_CONTINUOUS:          return "Zoom, Continuous";
653         case V4L2_CID_PRIVACY:                  return "Privacy";
654         case V4L2_CID_IRIS_ABSOLUTE:            return "Iris, Absolute";
655         case V4L2_CID_IRIS_RELATIVE:            return "Iris, Relative";
656         case V4L2_CID_AUTO_EXPOSURE_BIAS:       return "Auto Exposure, Bias";
657         case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
658         case V4L2_CID_WIDE_DYNAMIC_RANGE:       return "Wide Dynamic Range";
659         case V4L2_CID_IMAGE_STABILIZATION:      return "Image Stabilization";
660         case V4L2_CID_ISO_SENSITIVITY:          return "ISO Sensitivity";
661         case V4L2_CID_ISO_SENSITIVITY_AUTO:     return "ISO Sensitivity, Auto";
662         case V4L2_CID_EXPOSURE_METERING:        return "Exposure, Metering Mode";
663         case V4L2_CID_SCENE_MODE:               return "Scene Mode";
664         case V4L2_CID_3A_LOCK:                  return "3A Lock";
665
666         /* FM Radio Modulator control */
667         /* Keep the order of the 'case's the same as in videodev2.h! */
668         case V4L2_CID_FM_TX_CLASS:              return "FM Radio Modulator Controls";
669         case V4L2_CID_RDS_TX_DEVIATION:         return "RDS Signal Deviation";
670         case V4L2_CID_RDS_TX_PI:                return "RDS Program ID";
671         case V4L2_CID_RDS_TX_PTY:               return "RDS Program Type";
672         case V4L2_CID_RDS_TX_PS_NAME:           return "RDS PS Name";
673         case V4L2_CID_RDS_TX_RADIO_TEXT:        return "RDS Radio Text";
674         case V4L2_CID_AUDIO_LIMITER_ENABLED:    return "Audio Limiter Feature Enabled";
675         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
676         case V4L2_CID_AUDIO_LIMITER_DEVIATION:  return "Audio Limiter Deviation";
677         case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
678         case V4L2_CID_AUDIO_COMPRESSION_GAIN:   return "Audio Compression Gain";
679         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
680         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
681         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
682         case V4L2_CID_PILOT_TONE_ENABLED:       return "Pilot Tone Feature Enabled";
683         case V4L2_CID_PILOT_TONE_DEVIATION:     return "Pilot Tone Deviation";
684         case V4L2_CID_PILOT_TONE_FREQUENCY:     return "Pilot Tone Frequency";
685         case V4L2_CID_TUNE_PREEMPHASIS:         return "Pre-Emphasis";
686         case V4L2_CID_TUNE_POWER_LEVEL:         return "Tune Power Level";
687         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:   return "Tune Antenna Capacitor";
688
689         /* Flash controls */
690         case V4L2_CID_FLASH_CLASS:              return "Flash Controls";
691         case V4L2_CID_FLASH_LED_MODE:           return "LED Mode";
692         case V4L2_CID_FLASH_STROBE_SOURCE:      return "Strobe Source";
693         case V4L2_CID_FLASH_STROBE:             return "Strobe";
694         case V4L2_CID_FLASH_STROBE_STOP:        return "Stop Strobe";
695         case V4L2_CID_FLASH_STROBE_STATUS:      return "Strobe Status";
696         case V4L2_CID_FLASH_TIMEOUT:            return "Strobe Timeout";
697         case V4L2_CID_FLASH_INTENSITY:          return "Intensity, Flash Mode";
698         case V4L2_CID_FLASH_TORCH_INTENSITY:    return "Intensity, Torch Mode";
699         case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
700         case V4L2_CID_FLASH_FAULT:              return "Faults";
701         case V4L2_CID_FLASH_CHARGE:             return "Charge";
702         case V4L2_CID_FLASH_READY:              return "Ready to Strobe";
703
704         /* JPEG encoder controls */
705         /* Keep the order of the 'case's the same as in videodev2.h! */
706         case V4L2_CID_JPEG_CLASS:               return "JPEG Compression Controls";
707         case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:  return "Chroma Subsampling";
708         case V4L2_CID_JPEG_RESTART_INTERVAL:    return "Restart Interval";
709         case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
710         case V4L2_CID_JPEG_ACTIVE_MARKER:       return "Active Markers";
711
712         /* Image source controls */
713         case V4L2_CID_IMAGE_SOURCE_CLASS:       return "Image Source Controls";
714         case V4L2_CID_VBLANK:                   return "Vertical Blanking";
715         case V4L2_CID_HBLANK:                   return "Horizontal Blanking";
716         case V4L2_CID_ANALOGUE_GAIN:            return "Analogue Gain";
717
718         /* Image processing controls */
719         case V4L2_CID_IMAGE_PROC_CLASS:         return "Image Processing Controls";
720         case V4L2_CID_LINK_FREQ:                return "Link Frequency";
721         case V4L2_CID_PIXEL_RATE:               return "Pixel Rate";
722
723         default:
724                 return NULL;
725         }
726 }
727 EXPORT_SYMBOL(v4l2_ctrl_get_name);
728
729 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
730                     s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags)
731 {
732         *name = v4l2_ctrl_get_name(id);
733         *flags = 0;
734
735         switch (id) {
736         case V4L2_CID_AUDIO_MUTE:
737         case V4L2_CID_AUDIO_LOUDNESS:
738         case V4L2_CID_AUTO_WHITE_BALANCE:
739         case V4L2_CID_AUTOGAIN:
740         case V4L2_CID_HFLIP:
741         case V4L2_CID_VFLIP:
742         case V4L2_CID_HUE_AUTO:
743         case V4L2_CID_CHROMA_AGC:
744         case V4L2_CID_COLOR_KILLER:
745         case V4L2_CID_MPEG_AUDIO_MUTE:
746         case V4L2_CID_MPEG_VIDEO_MUTE:
747         case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
748         case V4L2_CID_MPEG_VIDEO_PULLDOWN:
749         case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
750         case V4L2_CID_FOCUS_AUTO:
751         case V4L2_CID_PRIVACY:
752         case V4L2_CID_AUDIO_LIMITER_ENABLED:
753         case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
754         case V4L2_CID_PILOT_TONE_ENABLED:
755         case V4L2_CID_ILLUMINATORS_1:
756         case V4L2_CID_ILLUMINATORS_2:
757         case V4L2_CID_FLASH_STROBE_STATUS:
758         case V4L2_CID_FLASH_CHARGE:
759         case V4L2_CID_FLASH_READY:
760         case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
761         case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
762         case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
763         case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
764         case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
765         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
766         case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
767         case V4L2_CID_WIDE_DYNAMIC_RANGE:
768         case V4L2_CID_IMAGE_STABILIZATION:
769                 *type = V4L2_CTRL_TYPE_BOOLEAN;
770                 *min = 0;
771                 *max = *step = 1;
772                 break;
773         case V4L2_CID_PAN_RESET:
774         case V4L2_CID_TILT_RESET:
775         case V4L2_CID_FLASH_STROBE:
776         case V4L2_CID_FLASH_STROBE_STOP:
777                 *type = V4L2_CTRL_TYPE_BUTTON;
778                 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
779                 *min = *max = *step = *def = 0;
780                 break;
781         case V4L2_CID_POWER_LINE_FREQUENCY:
782         case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
783         case V4L2_CID_MPEG_AUDIO_ENCODING:
784         case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
785         case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
786         case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
787         case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
788         case V4L2_CID_MPEG_AUDIO_MODE:
789         case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
790         case V4L2_CID_MPEG_AUDIO_EMPHASIS:
791         case V4L2_CID_MPEG_AUDIO_CRC:
792         case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
793         case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
794         case V4L2_CID_MPEG_VIDEO_ENCODING:
795         case V4L2_CID_MPEG_VIDEO_ASPECT:
796         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
797         case V4L2_CID_MPEG_STREAM_TYPE:
798         case V4L2_CID_MPEG_STREAM_VBI_FMT:
799         case V4L2_CID_EXPOSURE_AUTO:
800         case V4L2_CID_COLORFX:
801         case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
802         case V4L2_CID_TUNE_PREEMPHASIS:
803         case V4L2_CID_FLASH_LED_MODE:
804         case V4L2_CID_FLASH_STROBE_SOURCE:
805         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
806         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
807         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
808         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
809         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
810         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
811         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
812         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
813         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
814         case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
815         case V4L2_CID_ISO_SENSITIVITY_AUTO:
816         case V4L2_CID_EXPOSURE_METERING:
817         case V4L2_CID_SCENE_MODE:
818                 *type = V4L2_CTRL_TYPE_MENU;
819                 break;
820         case V4L2_CID_LINK_FREQ:
821                 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
822                 break;
823         case V4L2_CID_RDS_TX_PS_NAME:
824         case V4L2_CID_RDS_TX_RADIO_TEXT:
825                 *type = V4L2_CTRL_TYPE_STRING;
826                 break;
827         case V4L2_CID_ISO_SENSITIVITY:
828         case V4L2_CID_AUTO_EXPOSURE_BIAS:
829                 *type = V4L2_CTRL_TYPE_INTEGER_MENU;
830                 break;
831         case V4L2_CID_USER_CLASS:
832         case V4L2_CID_CAMERA_CLASS:
833         case V4L2_CID_MPEG_CLASS:
834         case V4L2_CID_FM_TX_CLASS:
835         case V4L2_CID_FLASH_CLASS:
836         case V4L2_CID_JPEG_CLASS:
837         case V4L2_CID_IMAGE_SOURCE_CLASS:
838         case V4L2_CID_IMAGE_PROC_CLASS:
839                 *type = V4L2_CTRL_TYPE_CTRL_CLASS;
840                 /* You can neither read not write these */
841                 *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
842                 *min = *max = *step = *def = 0;
843                 break;
844         case V4L2_CID_BG_COLOR:
845                 *type = V4L2_CTRL_TYPE_INTEGER;
846                 *step = 1;
847                 *min = 0;
848                 /* Max is calculated as RGB888 that is 2^24 */
849                 *max = 0xFFFFFF;
850                 break;
851         case V4L2_CID_FLASH_FAULT:
852         case V4L2_CID_JPEG_ACTIVE_MARKER:
853         case V4L2_CID_3A_LOCK:
854                 *type = V4L2_CTRL_TYPE_BITMASK;
855                 break;
856         case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
857         case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
858                 *type = V4L2_CTRL_TYPE_INTEGER;
859                 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
860                 break;
861         case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
862         case V4L2_CID_MPEG_VIDEO_DEC_PTS:
863                 *flags |= V4L2_CTRL_FLAG_VOLATILE;
864                 /* Fall through */
865         case V4L2_CID_PIXEL_RATE:
866                 *type = V4L2_CTRL_TYPE_INTEGER64;
867                 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
868                 *min = *max = *step = *def = 0;
869                 break;
870         default:
871                 *type = V4L2_CTRL_TYPE_INTEGER;
872                 break;
873         }
874         switch (id) {
875         case V4L2_CID_MPEG_AUDIO_ENCODING:
876         case V4L2_CID_MPEG_AUDIO_MODE:
877         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
878         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
879         case V4L2_CID_MPEG_STREAM_TYPE:
880                 *flags |= V4L2_CTRL_FLAG_UPDATE;
881                 break;
882         case V4L2_CID_AUDIO_VOLUME:
883         case V4L2_CID_AUDIO_BALANCE:
884         case V4L2_CID_AUDIO_BASS:
885         case V4L2_CID_AUDIO_TREBLE:
886         case V4L2_CID_BRIGHTNESS:
887         case V4L2_CID_CONTRAST:
888         case V4L2_CID_SATURATION:
889         case V4L2_CID_HUE:
890         case V4L2_CID_RED_BALANCE:
891         case V4L2_CID_BLUE_BALANCE:
892         case V4L2_CID_GAMMA:
893         case V4L2_CID_SHARPNESS:
894         case V4L2_CID_CHROMA_GAIN:
895         case V4L2_CID_RDS_TX_DEVIATION:
896         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
897         case V4L2_CID_AUDIO_LIMITER_DEVIATION:
898         case V4L2_CID_AUDIO_COMPRESSION_GAIN:
899         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
900         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
901         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
902         case V4L2_CID_PILOT_TONE_DEVIATION:
903         case V4L2_CID_PILOT_TONE_FREQUENCY:
904         case V4L2_CID_TUNE_POWER_LEVEL:
905         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
906                 *flags |= V4L2_CTRL_FLAG_SLIDER;
907                 break;
908         case V4L2_CID_PAN_RELATIVE:
909         case V4L2_CID_TILT_RELATIVE:
910         case V4L2_CID_FOCUS_RELATIVE:
911         case V4L2_CID_IRIS_RELATIVE:
912         case V4L2_CID_ZOOM_RELATIVE:
913                 *flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
914                 break;
915         case V4L2_CID_FLASH_STROBE_STATUS:
916         case V4L2_CID_FLASH_READY:
917                 *flags |= V4L2_CTRL_FLAG_READ_ONLY;
918                 break;
919         }
920 }
921 EXPORT_SYMBOL(v4l2_ctrl_fill);
922
923 /* Helper function to determine whether the control type is compatible with
924    VIDIOC_G/S_CTRL. */
925 static bool type_is_int(const struct v4l2_ctrl *ctrl)
926 {
927         switch (ctrl->type) {
928         case V4L2_CTRL_TYPE_INTEGER64:
929         case V4L2_CTRL_TYPE_STRING:
930                 /* Nope, these need v4l2_ext_control */
931                 return false;
932         default:
933                 return true;
934         }
935 }
936
937 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
938 {
939         memset(ev->reserved, 0, sizeof(ev->reserved));
940         ev->type = V4L2_EVENT_CTRL;
941         ev->id = ctrl->id;
942         ev->u.ctrl.changes = changes;
943         ev->u.ctrl.type = ctrl->type;
944         ev->u.ctrl.flags = ctrl->flags;
945         if (ctrl->type == V4L2_CTRL_TYPE_STRING)
946                 ev->u.ctrl.value64 = 0;
947         else
948                 ev->u.ctrl.value64 = ctrl->cur.val64;
949         ev->u.ctrl.minimum = ctrl->minimum;
950         ev->u.ctrl.maximum = ctrl->maximum;
951         if (ctrl->type == V4L2_CTRL_TYPE_MENU
952             || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
953                 ev->u.ctrl.step = 1;
954         else
955                 ev->u.ctrl.step = ctrl->step;
956         ev->u.ctrl.default_value = ctrl->default_value;
957 }
958
959 static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
960 {
961         struct v4l2_event ev;
962         struct v4l2_subscribed_event *sev;
963
964         if (list_empty(&ctrl->ev_subs))
965                 return;
966         fill_event(&ev, ctrl, changes);
967
968         list_for_each_entry(sev, &ctrl->ev_subs, node)
969                 if (sev->fh != fh ||
970                     (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
971                         v4l2_event_queue_fh(sev->fh, &ev);
972 }
973
974 /* Helper function: copy the current control value back to the caller */
975 static int cur_to_user(struct v4l2_ext_control *c,
976                        struct v4l2_ctrl *ctrl)
977 {
978         u32 len;
979
980         switch (ctrl->type) {
981         case V4L2_CTRL_TYPE_STRING:
982                 len = strlen(ctrl->cur.string);
983                 if (c->size < len + 1) {
984                         c->size = len + 1;
985                         return -ENOSPC;
986                 }
987                 return copy_to_user(c->string, ctrl->cur.string,
988                                                 len + 1) ? -EFAULT : 0;
989         case V4L2_CTRL_TYPE_INTEGER64:
990                 c->value64 = ctrl->cur.val64;
991                 break;
992         default:
993                 c->value = ctrl->cur.val;
994                 break;
995         }
996         return 0;
997 }
998
999 /* Helper function: copy the caller-provider value as the new control value */
1000 static int user_to_new(struct v4l2_ext_control *c,
1001                        struct v4l2_ctrl *ctrl)
1002 {
1003         int ret;
1004         u32 size;
1005
1006         ctrl->is_new = 1;
1007         switch (ctrl->type) {
1008         case V4L2_CTRL_TYPE_INTEGER64:
1009                 ctrl->val64 = c->value64;
1010                 break;
1011         case V4L2_CTRL_TYPE_STRING:
1012                 size = c->size;
1013                 if (size == 0)
1014                         return -ERANGE;
1015                 if (size > ctrl->maximum + 1)
1016                         size = ctrl->maximum + 1;
1017                 ret = copy_from_user(ctrl->string, c->string, size);
1018                 if (!ret) {
1019                         char last = ctrl->string[size - 1];
1020
1021                         ctrl->string[size - 1] = 0;
1022                         /* If the string was longer than ctrl->maximum,
1023                            then return an error. */
1024                         if (strlen(ctrl->string) == ctrl->maximum && last)
1025                                 return -ERANGE;
1026                 }
1027                 return ret ? -EFAULT : 0;
1028         default:
1029                 ctrl->val = c->value;
1030                 break;
1031         }
1032         return 0;
1033 }
1034
1035 /* Helper function: copy the new control value back to the caller */
1036 static int new_to_user(struct v4l2_ext_control *c,
1037                        struct v4l2_ctrl *ctrl)
1038 {
1039         u32 len;
1040
1041         switch (ctrl->type) {
1042         case V4L2_CTRL_TYPE_STRING:
1043                 len = strlen(ctrl->string);
1044                 if (c->size < len + 1) {
1045                         c->size = ctrl->maximum + 1;
1046                         return -ENOSPC;
1047                 }
1048                 return copy_to_user(c->string, ctrl->string,
1049                                                 len + 1) ? -EFAULT : 0;
1050         case V4L2_CTRL_TYPE_INTEGER64:
1051                 c->value64 = ctrl->val64;
1052                 break;
1053         default:
1054                 c->value = ctrl->val;
1055                 break;
1056         }
1057         return 0;
1058 }
1059
1060 /* Copy the new value to the current value. */
1061 static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
1062                                                 bool update_inactive)
1063 {
1064         bool changed = false;
1065
1066         if (ctrl == NULL)
1067                 return;
1068         switch (ctrl->type) {
1069         case V4L2_CTRL_TYPE_BUTTON:
1070                 changed = true;
1071                 break;
1072         case V4L2_CTRL_TYPE_STRING:
1073                 /* strings are always 0-terminated */
1074                 changed = strcmp(ctrl->string, ctrl->cur.string);
1075                 strcpy(ctrl->cur.string, ctrl->string);
1076                 break;
1077         case V4L2_CTRL_TYPE_INTEGER64:
1078                 changed = ctrl->val64 != ctrl->cur.val64;
1079                 ctrl->cur.val64 = ctrl->val64;
1080                 break;
1081         default:
1082                 changed = ctrl->val != ctrl->cur.val;
1083                 ctrl->cur.val = ctrl->val;
1084                 break;
1085         }
1086         if (update_inactive) {
1087                 /* Note: update_inactive can only be true for auto clusters. */
1088                 ctrl->flags &=
1089                         ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
1090                 if (!is_cur_manual(ctrl->cluster[0])) {
1091                         ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1092                         if (ctrl->cluster[0]->has_volatiles)
1093                                 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1094                 }
1095                 fh = NULL;
1096         }
1097         if (changed || update_inactive) {
1098                 /* If a control was changed that was not one of the controls
1099                    modified by the application, then send the event to all. */
1100                 if (!ctrl->is_new)
1101                         fh = NULL;
1102                 send_event(fh, ctrl,
1103                         (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) |
1104                         (update_inactive ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
1105         }
1106 }
1107
1108 /* Copy the current value to the new value */
1109 static void cur_to_new(struct v4l2_ctrl *ctrl)
1110 {
1111         if (ctrl == NULL)
1112                 return;
1113         switch (ctrl->type) {
1114         case V4L2_CTRL_TYPE_STRING:
1115                 /* strings are always 0-terminated */
1116                 strcpy(ctrl->string, ctrl->cur.string);
1117                 break;
1118         case V4L2_CTRL_TYPE_INTEGER64:
1119                 ctrl->val64 = ctrl->cur.val64;
1120                 break;
1121         default:
1122                 ctrl->val = ctrl->cur.val;
1123                 break;
1124         }
1125 }
1126
1127 /* Return non-zero if one or more of the controls in the cluster has a new
1128    value that differs from the current value. */
1129 static int cluster_changed(struct v4l2_ctrl *master)
1130 {
1131         int diff = 0;
1132         int i;
1133
1134         for (i = 0; !diff && i < master->ncontrols; i++) {
1135                 struct v4l2_ctrl *ctrl = master->cluster[i];
1136
1137                 if (ctrl == NULL)
1138                         continue;
1139                 switch (ctrl->type) {
1140                 case V4L2_CTRL_TYPE_BUTTON:
1141                         /* Button controls are always 'different' */
1142                         return 1;
1143                 case V4L2_CTRL_TYPE_STRING:
1144                         /* strings are always 0-terminated */
1145                         diff = strcmp(ctrl->string, ctrl->cur.string);
1146                         break;
1147                 case V4L2_CTRL_TYPE_INTEGER64:
1148                         diff = ctrl->val64 != ctrl->cur.val64;
1149                         break;
1150                 default:
1151                         diff = ctrl->val != ctrl->cur.val;
1152                         break;
1153                 }
1154         }
1155         return diff;
1156 }
1157
1158 /* Validate integer-type control */
1159 static int validate_new_int(const struct v4l2_ctrl *ctrl, s32 *pval)
1160 {
1161         s32 val = *pval;
1162         u32 offset;
1163
1164         switch (ctrl->type) {
1165         case V4L2_CTRL_TYPE_INTEGER:
1166                 /* Round towards the closest legal value */
1167                 val += ctrl->step / 2;
1168                 if (val < ctrl->minimum)
1169                         val = ctrl->minimum;
1170                 if (val > ctrl->maximum)
1171                         val = ctrl->maximum;
1172                 offset = val - ctrl->minimum;
1173                 offset = ctrl->step * (offset / ctrl->step);
1174                 val = ctrl->minimum + offset;
1175                 *pval = val;
1176                 return 0;
1177
1178         case V4L2_CTRL_TYPE_BOOLEAN:
1179                 *pval = !!val;
1180                 return 0;
1181
1182         case V4L2_CTRL_TYPE_MENU:
1183         case V4L2_CTRL_TYPE_INTEGER_MENU:
1184                 if (val < ctrl->minimum || val > ctrl->maximum)
1185                         return -ERANGE;
1186                 if (ctrl->menu_skip_mask & (1 << val))
1187                         return -EINVAL;
1188                 if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
1189                     ctrl->qmenu[val][0] == '\0')
1190                         return -EINVAL;
1191                 return 0;
1192
1193         case V4L2_CTRL_TYPE_BITMASK:
1194                 *pval &= ctrl->maximum;
1195                 return 0;
1196
1197         case V4L2_CTRL_TYPE_BUTTON:
1198         case V4L2_CTRL_TYPE_CTRL_CLASS:
1199                 *pval = 0;
1200                 return 0;
1201
1202         default:
1203                 return -EINVAL;
1204         }
1205 }
1206
1207 /* Validate a new control */
1208 static int validate_new(const struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
1209 {
1210         char *s = c->string;
1211         size_t len;
1212
1213         switch (ctrl->type) {
1214         case V4L2_CTRL_TYPE_INTEGER:
1215         case V4L2_CTRL_TYPE_BOOLEAN:
1216         case V4L2_CTRL_TYPE_MENU:
1217         case V4L2_CTRL_TYPE_INTEGER_MENU:
1218         case V4L2_CTRL_TYPE_BITMASK:
1219         case V4L2_CTRL_TYPE_BUTTON:
1220         case V4L2_CTRL_TYPE_CTRL_CLASS:
1221                 return validate_new_int(ctrl, &c->value);
1222
1223         case V4L2_CTRL_TYPE_INTEGER64:
1224                 return 0;
1225
1226         case V4L2_CTRL_TYPE_STRING:
1227                 len = strlen(s);
1228                 if (len < ctrl->minimum)
1229                         return -ERANGE;
1230                 if ((len - ctrl->minimum) % ctrl->step)
1231                         return -ERANGE;
1232                 return 0;
1233
1234         default:
1235                 return -EINVAL;
1236         }
1237 }
1238
1239 static inline u32 node2id(struct list_head *node)
1240 {
1241         return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
1242 }
1243
1244 /* Set the handler's error code if it wasn't set earlier already */
1245 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
1246 {
1247         if (hdl->error == 0)
1248                 hdl->error = err;
1249         return err;
1250 }
1251
1252 /* Initialize the handler */
1253 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
1254                            unsigned nr_of_controls_hint)
1255 {
1256         hdl->lock = &hdl->_lock;
1257         mutex_init(hdl->lock);
1258         INIT_LIST_HEAD(&hdl->ctrls);
1259         INIT_LIST_HEAD(&hdl->ctrl_refs);
1260         hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
1261         hdl->buckets = kcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
1262                                GFP_KERNEL);
1263         hdl->error = hdl->buckets ? 0 : -ENOMEM;
1264         return hdl->error;
1265 }
1266 EXPORT_SYMBOL(v4l2_ctrl_handler_init);
1267
1268 /* Free all controls and control refs */
1269 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
1270 {
1271         struct v4l2_ctrl_ref *ref, *next_ref;
1272         struct v4l2_ctrl *ctrl, *next_ctrl;
1273         struct v4l2_subscribed_event *sev, *next_sev;
1274
1275         if (hdl == NULL || hdl->buckets == NULL)
1276                 return;
1277
1278         mutex_lock(hdl->lock);
1279         /* Free all nodes */
1280         list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
1281                 list_del(&ref->node);
1282                 kfree(ref);
1283         }
1284         /* Free all controls owned by the handler */
1285         list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
1286                 list_del(&ctrl->node);
1287                 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
1288                         list_del(&sev->node);
1289                 kfree(ctrl);
1290         }
1291         kfree(hdl->buckets);
1292         hdl->buckets = NULL;
1293         hdl->cached = NULL;
1294         hdl->error = 0;
1295         mutex_unlock(hdl->lock);
1296 }
1297 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
1298
1299 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
1300    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
1301    with applications that do not use the NEXT_CTRL flag.
1302
1303    We just find the n-th private user control. It's O(N), but that should not
1304    be an issue in this particular case. */
1305 static struct v4l2_ctrl_ref *find_private_ref(
1306                 struct v4l2_ctrl_handler *hdl, u32 id)
1307 {
1308         struct v4l2_ctrl_ref *ref;
1309
1310         id -= V4L2_CID_PRIVATE_BASE;
1311         list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1312                 /* Search for private user controls that are compatible with
1313                    VIDIOC_G/S_CTRL. */
1314                 if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
1315                     V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
1316                         if (!type_is_int(ref->ctrl))
1317                                 continue;
1318                         if (id == 0)
1319                                 return ref;
1320                         id--;
1321                 }
1322         }
1323         return NULL;
1324 }
1325
1326 /* Find a control with the given ID. */
1327 static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
1328 {
1329         struct v4l2_ctrl_ref *ref;
1330         int bucket;
1331
1332         id &= V4L2_CTRL_ID_MASK;
1333
1334         /* Old-style private controls need special handling */
1335         if (id >= V4L2_CID_PRIVATE_BASE)
1336                 return find_private_ref(hdl, id);
1337         bucket = id % hdl->nr_of_buckets;
1338
1339         /* Simple optimization: cache the last control found */
1340         if (hdl->cached && hdl->cached->ctrl->id == id)
1341                 return hdl->cached;
1342
1343         /* Not in cache, search the hash */
1344         ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
1345         while (ref && ref->ctrl->id != id)
1346                 ref = ref->next;
1347
1348         if (ref)
1349                 hdl->cached = ref; /* cache it! */
1350         return ref;
1351 }
1352
1353 /* Find a control with the given ID. Take the handler's lock first. */
1354 static struct v4l2_ctrl_ref *find_ref_lock(
1355                 struct v4l2_ctrl_handler *hdl, u32 id)
1356 {
1357         struct v4l2_ctrl_ref *ref = NULL;
1358
1359         if (hdl) {
1360                 mutex_lock(hdl->lock);
1361                 ref = find_ref(hdl, id);
1362                 mutex_unlock(hdl->lock);
1363         }
1364         return ref;
1365 }
1366
1367 /* Find a control with the given ID. */
1368 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
1369 {
1370         struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
1371
1372         return ref ? ref->ctrl : NULL;
1373 }
1374 EXPORT_SYMBOL(v4l2_ctrl_find);
1375
1376 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
1377 static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
1378                            struct v4l2_ctrl *ctrl)
1379 {
1380         struct v4l2_ctrl_ref *ref;
1381         struct v4l2_ctrl_ref *new_ref;
1382         u32 id = ctrl->id;
1383         u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
1384         int bucket = id % hdl->nr_of_buckets;   /* which bucket to use */
1385
1386         /* Automatically add the control class if it is not yet present. */
1387         if (id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
1388                 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
1389                         return hdl->error;
1390
1391         if (hdl->error)
1392                 return hdl->error;
1393
1394         new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
1395         if (!new_ref)
1396                 return handler_set_err(hdl, -ENOMEM);
1397         new_ref->ctrl = ctrl;
1398         if (ctrl->handler == hdl) {
1399                 /* By default each control starts in a cluster of its own.
1400                    new_ref->ctrl is basically a cluster array with one
1401                    element, so that's perfect to use as the cluster pointer.
1402                    But only do this for the handler that owns the control. */
1403                 ctrl->cluster = &new_ref->ctrl;
1404                 ctrl->ncontrols = 1;
1405         }
1406
1407         INIT_LIST_HEAD(&new_ref->node);
1408
1409         mutex_lock(hdl->lock);
1410
1411         /* Add immediately at the end of the list if the list is empty, or if
1412            the last element in the list has a lower ID.
1413            This ensures that when elements are added in ascending order the
1414            insertion is an O(1) operation. */
1415         if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
1416                 list_add_tail(&new_ref->node, &hdl->ctrl_refs);
1417                 goto insert_in_hash;
1418         }
1419
1420         /* Find insert position in sorted list */
1421         list_for_each_entry(ref, &hdl->ctrl_refs, node) {
1422                 if (ref->ctrl->id < id)
1423                         continue;
1424                 /* Don't add duplicates */
1425                 if (ref->ctrl->id == id) {
1426                         kfree(new_ref);
1427                         goto unlock;
1428                 }
1429                 list_add(&new_ref->node, ref->node.prev);
1430                 break;
1431         }
1432
1433 insert_in_hash:
1434         /* Insert the control node in the hash */
1435         new_ref->next = hdl->buckets[bucket];
1436         hdl->buckets[bucket] = new_ref;
1437
1438 unlock:
1439         mutex_unlock(hdl->lock);
1440         return 0;
1441 }
1442
1443 /* Add a new control */
1444 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
1445                         const struct v4l2_ctrl_ops *ops,
1446                         u32 id, const char *name, enum v4l2_ctrl_type type,
1447                         s32 min, s32 max, u32 step, s32 def,
1448                         u32 flags, const char * const *qmenu,
1449                         const s64 *qmenu_int, void *priv)
1450 {
1451         struct v4l2_ctrl *ctrl;
1452         unsigned sz_extra = 0;
1453
1454         if (hdl->error)
1455                 return NULL;
1456
1457         /* Sanity checks */
1458         if (id == 0 || name == NULL || id >= V4L2_CID_PRIVATE_BASE ||
1459             (type == V4L2_CTRL_TYPE_INTEGER && step == 0) ||
1460             (type == V4L2_CTRL_TYPE_BITMASK && max == 0) ||
1461             (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
1462             (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL) ||
1463             (type == V4L2_CTRL_TYPE_STRING && max == 0)) {
1464                 handler_set_err(hdl, -ERANGE);
1465                 return NULL;
1466         }
1467         if (type != V4L2_CTRL_TYPE_BITMASK && max < min) {
1468                 handler_set_err(hdl, -ERANGE);
1469                 return NULL;
1470         }
1471         if ((type == V4L2_CTRL_TYPE_INTEGER ||
1472              type == V4L2_CTRL_TYPE_MENU ||
1473              type == V4L2_CTRL_TYPE_INTEGER_MENU ||
1474              type == V4L2_CTRL_TYPE_BOOLEAN) &&
1475             (def < min || def > max)) {
1476                 handler_set_err(hdl, -ERANGE);
1477                 return NULL;
1478         }
1479         if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
1480                 handler_set_err(hdl, -ERANGE);
1481                 return NULL;
1482         }
1483
1484         if (type == V4L2_CTRL_TYPE_BUTTON)
1485                 flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1486         else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
1487                 flags |= V4L2_CTRL_FLAG_READ_ONLY;
1488         else if (type == V4L2_CTRL_TYPE_STRING)
1489                 sz_extra += 2 * (max + 1);
1490
1491         ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
1492         if (ctrl == NULL) {
1493                 handler_set_err(hdl, -ENOMEM);
1494                 return NULL;
1495         }
1496
1497         INIT_LIST_HEAD(&ctrl->node);
1498         INIT_LIST_HEAD(&ctrl->ev_subs);
1499         ctrl->handler = hdl;
1500         ctrl->ops = ops;
1501         ctrl->id = id;
1502         ctrl->name = name;
1503         ctrl->type = type;
1504         ctrl->flags = flags;
1505         ctrl->minimum = min;
1506         ctrl->maximum = max;
1507         ctrl->step = step;
1508         if (type == V4L2_CTRL_TYPE_MENU)
1509                 ctrl->qmenu = qmenu;
1510         else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
1511                 ctrl->qmenu_int = qmenu_int;
1512         ctrl->priv = priv;
1513         ctrl->cur.val = ctrl->val = ctrl->default_value = def;
1514
1515         if (ctrl->type == V4L2_CTRL_TYPE_STRING) {
1516                 ctrl->cur.string = (char *)&ctrl[1] + sz_extra - (max + 1);
1517                 ctrl->string = (char *)&ctrl[1] + sz_extra - 2 * (max + 1);
1518                 if (ctrl->minimum)
1519                         memset(ctrl->cur.string, ' ', ctrl->minimum);
1520         }
1521         if (handler_new_ref(hdl, ctrl)) {
1522                 kfree(ctrl);
1523                 return NULL;
1524         }
1525         mutex_lock(hdl->lock);
1526         list_add_tail(&ctrl->node, &hdl->ctrls);
1527         mutex_unlock(hdl->lock);
1528         return ctrl;
1529 }
1530
1531 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
1532                         const struct v4l2_ctrl_config *cfg, void *priv)
1533 {
1534         bool is_menu;
1535         struct v4l2_ctrl *ctrl;
1536         const char *name = cfg->name;
1537         const char * const *qmenu = cfg->qmenu;
1538         const s64 *qmenu_int = cfg->qmenu_int;
1539         enum v4l2_ctrl_type type = cfg->type;
1540         u32 flags = cfg->flags;
1541         s32 min = cfg->min;
1542         s32 max = cfg->max;
1543         u32 step = cfg->step;
1544         s32 def = cfg->def;
1545
1546         if (name == NULL)
1547                 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
1548                                                                 &def, &flags);
1549
1550         is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
1551                    cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
1552         if (is_menu)
1553                 WARN_ON(step);
1554         else
1555                 WARN_ON(cfg->menu_skip_mask);
1556         if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
1557                 qmenu = v4l2_ctrl_get_menu(cfg->id);
1558         else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
1559                  qmenu_int == NULL) {
1560                 handler_set_err(hdl, -EINVAL);
1561                 return NULL;
1562         }
1563
1564         ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->id, name,
1565                         type, min, max,
1566                         is_menu ? cfg->menu_skip_mask : step,
1567                         def, flags, qmenu, qmenu_int, priv);
1568         if (ctrl)
1569                 ctrl->is_private = cfg->is_private;
1570         return ctrl;
1571 }
1572 EXPORT_SYMBOL(v4l2_ctrl_new_custom);
1573
1574 /* Helper function for standard non-menu controls */
1575 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
1576                         const struct v4l2_ctrl_ops *ops,
1577                         u32 id, s32 min, s32 max, u32 step, s32 def)
1578 {
1579         const char *name;
1580         enum v4l2_ctrl_type type;
1581         u32 flags;
1582
1583         v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1584         if (type == V4L2_CTRL_TYPE_MENU
1585             || type == V4L2_CTRL_TYPE_INTEGER_MENU) {
1586                 handler_set_err(hdl, -EINVAL);
1587                 return NULL;
1588         }
1589         return v4l2_ctrl_new(hdl, ops, id, name, type,
1590                              min, max, step, def, flags, NULL, NULL, NULL);
1591 }
1592 EXPORT_SYMBOL(v4l2_ctrl_new_std);
1593
1594 /* Helper function for standard menu controls */
1595 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
1596                         const struct v4l2_ctrl_ops *ops,
1597                         u32 id, s32 max, s32 mask, s32 def)
1598 {
1599         const char * const *qmenu = v4l2_ctrl_get_menu(id);
1600         const char *name;
1601         enum v4l2_ctrl_type type;
1602         s32 min;
1603         s32 step;
1604         u32 flags;
1605
1606         v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1607         if (type != V4L2_CTRL_TYPE_MENU) {
1608                 handler_set_err(hdl, -EINVAL);
1609                 return NULL;
1610         }
1611         return v4l2_ctrl_new(hdl, ops, id, name, type,
1612                              0, max, mask, def, flags, qmenu, NULL, NULL);
1613 }
1614 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
1615
1616 /* Helper function for standard integer menu controls */
1617 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
1618                         const struct v4l2_ctrl_ops *ops,
1619                         u32 id, s32 max, s32 def, const s64 *qmenu_int)
1620 {
1621         const char *name;
1622         enum v4l2_ctrl_type type;
1623         s32 min;
1624         s32 step;
1625         u32 flags;
1626
1627         v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
1628         if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
1629                 handler_set_err(hdl, -EINVAL);
1630                 return NULL;
1631         }
1632         return v4l2_ctrl_new(hdl, ops, id, name, type,
1633                              0, max, 0, def, flags, NULL, qmenu_int, NULL);
1634 }
1635 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
1636
1637 /* Add a control from another handler to this handler */
1638 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
1639                                           struct v4l2_ctrl *ctrl)
1640 {
1641         if (hdl == NULL || hdl->error)
1642                 return NULL;
1643         if (ctrl == NULL) {
1644                 handler_set_err(hdl, -EINVAL);
1645                 return NULL;
1646         }
1647         if (ctrl->handler == hdl)
1648                 return ctrl;
1649         return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
1650 }
1651 EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
1652
1653 /* Add the controls from another handler to our own. */
1654 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
1655                           struct v4l2_ctrl_handler *add)
1656 {
1657         struct v4l2_ctrl_ref *ref;
1658         int ret = 0;
1659
1660         /* Do nothing if either handler is NULL or if they are the same */
1661         if (!hdl || !add || hdl == add)
1662                 return 0;
1663         if (hdl->error)
1664                 return hdl->error;
1665         mutex_lock(add->lock);
1666         list_for_each_entry(ref, &add->ctrl_refs, node) {
1667                 struct v4l2_ctrl *ctrl = ref->ctrl;
1668
1669                 /* Skip handler-private controls. */
1670                 if (ctrl->is_private)
1671                         continue;
1672                 /* And control classes */
1673                 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1674                         continue;
1675                 ret = handler_new_ref(hdl, ctrl);
1676                 if (ret)
1677                         break;
1678         }
1679         mutex_unlock(add->lock);
1680         return ret;
1681 }
1682 EXPORT_SYMBOL(v4l2_ctrl_add_handler);
1683
1684 /* Cluster controls */
1685 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
1686 {
1687         bool has_volatiles = false;
1688         int i;
1689
1690         /* The first control is the master control and it must not be NULL */
1691         BUG_ON(ncontrols == 0 || controls[0] == NULL);
1692
1693         for (i = 0; i < ncontrols; i++) {
1694                 if (controls[i]) {
1695                         controls[i]->cluster = controls;
1696                         controls[i]->ncontrols = ncontrols;
1697                         if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
1698                                 has_volatiles = true;
1699                 }
1700         }
1701         controls[0]->has_volatiles = has_volatiles;
1702 }
1703 EXPORT_SYMBOL(v4l2_ctrl_cluster);
1704
1705 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
1706                             u8 manual_val, bool set_volatile)
1707 {
1708         struct v4l2_ctrl *master = controls[0];
1709         u32 flag = 0;
1710         int i;
1711
1712         v4l2_ctrl_cluster(ncontrols, controls);
1713         WARN_ON(ncontrols <= 1);
1714         WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
1715         WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
1716         master->is_auto = true;
1717         master->has_volatiles = set_volatile;
1718         master->manual_mode_value = manual_val;
1719         master->flags |= V4L2_CTRL_FLAG_UPDATE;
1720
1721         if (!is_cur_manual(master))
1722                 flag = V4L2_CTRL_FLAG_INACTIVE |
1723                         (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
1724
1725         for (i = 1; i < ncontrols; i++)
1726                 if (controls[i])
1727                         controls[i]->flags |= flag;
1728 }
1729 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
1730
1731 /* Activate/deactivate a control. */
1732 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
1733 {
1734         /* invert since the actual flag is called 'inactive' */
1735         bool inactive = !active;
1736         bool old;
1737
1738         if (ctrl == NULL)
1739                 return;
1740
1741         if (inactive)
1742                 /* set V4L2_CTRL_FLAG_INACTIVE */
1743                 old = test_and_set_bit(4, &ctrl->flags);
1744         else
1745                 /* clear V4L2_CTRL_FLAG_INACTIVE */
1746                 old = test_and_clear_bit(4, &ctrl->flags);
1747         if (old != inactive)
1748                 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1749 }
1750 EXPORT_SYMBOL(v4l2_ctrl_activate);
1751
1752 /* Grab/ungrab a control.
1753    Typically used when streaming starts and you want to grab controls,
1754    preventing the user from changing them.
1755
1756    Just call this and the framework will block any attempts to change
1757    these controls. */
1758 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
1759 {
1760         bool old;
1761
1762         if (ctrl == NULL)
1763                 return;
1764
1765         v4l2_ctrl_lock(ctrl);
1766         if (grabbed)
1767                 /* set V4L2_CTRL_FLAG_GRABBED */
1768                 old = test_and_set_bit(1, &ctrl->flags);
1769         else
1770                 /* clear V4L2_CTRL_FLAG_GRABBED */
1771                 old = test_and_clear_bit(1, &ctrl->flags);
1772         if (old != grabbed)
1773                 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
1774         v4l2_ctrl_unlock(ctrl);
1775 }
1776 EXPORT_SYMBOL(v4l2_ctrl_grab);
1777
1778 /* Log the control name and value */
1779 static void log_ctrl(const struct v4l2_ctrl *ctrl,
1780                      const char *prefix, const char *colon)
1781 {
1782         if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
1783                 return;
1784         if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
1785                 return;
1786
1787         printk(KERN_INFO "%s%s%s: ", prefix, colon, ctrl->name);
1788
1789         switch (ctrl->type) {
1790         case V4L2_CTRL_TYPE_INTEGER:
1791                 printk(KERN_CONT "%d", ctrl->cur.val);
1792                 break;
1793         case V4L2_CTRL_TYPE_BOOLEAN:
1794                 printk(KERN_CONT "%s", ctrl->cur.val ? "true" : "false");
1795                 break;
1796         case V4L2_CTRL_TYPE_MENU:
1797                 printk(KERN_CONT "%s", ctrl->qmenu[ctrl->cur.val]);
1798                 break;
1799         case V4L2_CTRL_TYPE_INTEGER_MENU:
1800                 printk(KERN_CONT "%lld", ctrl->qmenu_int[ctrl->cur.val]);
1801                 break;
1802         case V4L2_CTRL_TYPE_BITMASK:
1803                 printk(KERN_CONT "0x%08x", ctrl->cur.val);
1804                 break;
1805         case V4L2_CTRL_TYPE_INTEGER64:
1806                 printk(KERN_CONT "%lld", ctrl->cur.val64);
1807                 break;
1808         case V4L2_CTRL_TYPE_STRING:
1809                 printk(KERN_CONT "%s", ctrl->cur.string);
1810                 break;
1811         default:
1812                 printk(KERN_CONT "unknown type %d", ctrl->type);
1813                 break;
1814         }
1815         if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
1816                            V4L2_CTRL_FLAG_GRABBED |
1817                            V4L2_CTRL_FLAG_VOLATILE)) {
1818                 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1819                         printk(KERN_CONT " inactive");
1820                 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
1821                         printk(KERN_CONT " grabbed");
1822                 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
1823                         printk(KERN_CONT " volatile");
1824         }
1825         printk(KERN_CONT "\n");
1826 }
1827
1828 /* Log all controls owned by the handler */
1829 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
1830                                   const char *prefix)
1831 {
1832         struct v4l2_ctrl *ctrl;
1833         const char *colon = "";
1834         int len;
1835
1836         if (hdl == NULL)
1837                 return;
1838         if (prefix == NULL)
1839                 prefix = "";
1840         len = strlen(prefix);
1841         if (len && prefix[len - 1] != ' ')
1842                 colon = ": ";
1843         mutex_lock(hdl->lock);
1844         list_for_each_entry(ctrl, &hdl->ctrls, node)
1845                 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
1846                         log_ctrl(ctrl, prefix, colon);
1847         mutex_unlock(hdl->lock);
1848 }
1849 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
1850
1851 /* Call s_ctrl for all controls owned by the handler */
1852 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
1853 {
1854         struct v4l2_ctrl *ctrl;
1855         int ret = 0;
1856
1857         if (hdl == NULL)
1858                 return 0;
1859         mutex_lock(hdl->lock);
1860         list_for_each_entry(ctrl, &hdl->ctrls, node)
1861                 ctrl->done = false;
1862
1863         list_for_each_entry(ctrl, &hdl->ctrls, node) {
1864                 struct v4l2_ctrl *master = ctrl->cluster[0];
1865                 int i;
1866
1867                 /* Skip if this control was already handled by a cluster. */
1868                 /* Skip button controls and read-only controls. */
1869                 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
1870                     (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
1871                         continue;
1872
1873                 for (i = 0; i < master->ncontrols; i++) {
1874                         if (master->cluster[i]) {
1875                                 cur_to_new(master->cluster[i]);
1876                                 master->cluster[i]->is_new = 1;
1877                                 master->cluster[i]->done = true;
1878                         }
1879                 }
1880                 ret = call_op(master, s_ctrl);
1881                 if (ret)
1882                         break;
1883         }
1884         mutex_unlock(hdl->lock);
1885         return ret;
1886 }
1887 EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
1888
1889 /* Implement VIDIOC_QUERYCTRL */
1890 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
1891 {
1892         u32 id = qc->id & V4L2_CTRL_ID_MASK;
1893         struct v4l2_ctrl_ref *ref;
1894         struct v4l2_ctrl *ctrl;
1895
1896         if (hdl == NULL)
1897                 return -EINVAL;
1898
1899         mutex_lock(hdl->lock);
1900
1901         /* Try to find it */
1902         ref = find_ref(hdl, id);
1903
1904         if ((qc->id & V4L2_CTRL_FLAG_NEXT_CTRL) && !list_empty(&hdl->ctrl_refs)) {
1905                 /* Find the next control with ID > qc->id */
1906
1907                 /* Did we reach the end of the control list? */
1908                 if (id >= node2id(hdl->ctrl_refs.prev)) {
1909                         ref = NULL; /* Yes, so there is no next control */
1910                 } else if (ref) {
1911                         /* We found a control with the given ID, so just get
1912                            the next one in the list. */
1913                         ref = list_entry(ref->node.next, typeof(*ref), node);
1914                 } else {
1915                         /* No control with the given ID exists, so start
1916                            searching for the next largest ID. We know there
1917                            is one, otherwise the first 'if' above would have
1918                            been true. */
1919                         list_for_each_entry(ref, &hdl->ctrl_refs, node)
1920                                 if (id < ref->ctrl->id)
1921                                         break;
1922                 }
1923         }
1924         mutex_unlock(hdl->lock);
1925         if (!ref)
1926                 return -EINVAL;
1927
1928         ctrl = ref->ctrl;
1929         memset(qc, 0, sizeof(*qc));
1930         if (id >= V4L2_CID_PRIVATE_BASE)
1931                 qc->id = id;
1932         else
1933                 qc->id = ctrl->id;
1934         strlcpy(qc->name, ctrl->name, sizeof(qc->name));
1935         qc->minimum = ctrl->minimum;
1936         qc->maximum = ctrl->maximum;
1937         qc->default_value = ctrl->default_value;
1938         if (ctrl->type == V4L2_CTRL_TYPE_MENU
1939             || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
1940                 qc->step = 1;
1941         else
1942                 qc->step = ctrl->step;
1943         qc->flags = ctrl->flags;
1944         qc->type = ctrl->type;
1945         return 0;
1946 }
1947 EXPORT_SYMBOL(v4l2_queryctrl);
1948
1949 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1950 {
1951         if (qc->id & V4L2_CTRL_FLAG_NEXT_CTRL)
1952                 return -EINVAL;
1953         return v4l2_queryctrl(sd->ctrl_handler, qc);
1954 }
1955 EXPORT_SYMBOL(v4l2_subdev_queryctrl);
1956
1957 /* Implement VIDIOC_QUERYMENU */
1958 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
1959 {
1960         struct v4l2_ctrl *ctrl;
1961         u32 i = qm->index;
1962
1963         ctrl = v4l2_ctrl_find(hdl, qm->id);
1964         if (!ctrl)
1965                 return -EINVAL;
1966
1967         qm->reserved = 0;
1968         /* Sanity checks */
1969         switch (ctrl->type) {
1970         case V4L2_CTRL_TYPE_MENU:
1971                 if (ctrl->qmenu == NULL)
1972                         return -EINVAL;
1973                 break;
1974         case V4L2_CTRL_TYPE_INTEGER_MENU:
1975                 if (ctrl->qmenu_int == NULL)
1976                         return -EINVAL;
1977                 break;
1978         default:
1979                 return -EINVAL;
1980         }
1981
1982         if (i < ctrl->minimum || i > ctrl->maximum)
1983                 return -EINVAL;
1984
1985         /* Use mask to see if this menu item should be skipped */
1986         if (ctrl->menu_skip_mask & (1 << i))
1987                 return -EINVAL;
1988         /* Empty menu items should also be skipped */
1989         if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
1990                 if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
1991                         return -EINVAL;
1992                 strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
1993         } else {
1994                 qm->value = ctrl->qmenu_int[i];
1995         }
1996         return 0;
1997 }
1998 EXPORT_SYMBOL(v4l2_querymenu);
1999
2000 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
2001 {
2002         return v4l2_querymenu(sd->ctrl_handler, qm);
2003 }
2004 EXPORT_SYMBOL(v4l2_subdev_querymenu);
2005
2006
2007
2008 /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
2009
2010    It is not a fully atomic operation, just best-effort only. After all, if
2011    multiple controls have to be set through multiple i2c writes (for example)
2012    then some initial writes may succeed while others fail. Thus leaving the
2013    system in an inconsistent state. The question is how much effort you are
2014    willing to spend on trying to make something atomic that really isn't.
2015
2016    From the point of view of an application the main requirement is that
2017    when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
2018    error should be returned without actually affecting any controls.
2019
2020    If all the values are correct, then it is acceptable to just give up
2021    in case of low-level errors.
2022
2023    It is important though that the application can tell when only a partial
2024    configuration was done. The way we do that is through the error_idx field
2025    of struct v4l2_ext_controls: if that is equal to the count field then no
2026    controls were affected. Otherwise all controls before that index were
2027    successful in performing their 'get' or 'set' operation, the control at
2028    the given index failed, and you don't know what happened with the controls
2029    after the failed one. Since if they were part of a control cluster they
2030    could have been successfully processed (if a cluster member was encountered
2031    at index < error_idx), they could have failed (if a cluster member was at
2032    error_idx), or they may not have been processed yet (if the first cluster
2033    member appeared after error_idx).
2034
2035    It is all fairly theoretical, though. In practice all you can do is to
2036    bail out. If error_idx == count, then it is an application bug. If
2037    error_idx < count then it is only an application bug if the error code was
2038    EBUSY. That usually means that something started streaming just when you
2039    tried to set the controls. In all other cases it is a driver/hardware
2040    problem and all you can do is to retry or bail out.
2041
2042    Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
2043    never modifies controls the error_idx is just set to whatever control
2044    has an invalid value.
2045  */
2046
2047 /* Prepare for the extended g/s/try functions.
2048    Find the controls in the control array and do some basic checks. */
2049 static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
2050                              struct v4l2_ext_controls *cs,
2051                              struct v4l2_ctrl_helper *helpers)
2052 {
2053         struct v4l2_ctrl_helper *h;
2054         bool have_clusters = false;
2055         u32 i;
2056
2057         for (i = 0, h = helpers; i < cs->count; i++, h++) {
2058                 struct v4l2_ext_control *c = &cs->controls[i];
2059                 struct v4l2_ctrl_ref *ref;
2060                 struct v4l2_ctrl *ctrl;
2061                 u32 id = c->id & V4L2_CTRL_ID_MASK;
2062
2063                 cs->error_idx = i;
2064
2065                 if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
2066                         return -EINVAL;
2067
2068                 /* Old-style private controls are not allowed for
2069                    extended controls */
2070                 if (id >= V4L2_CID_PRIVATE_BASE)
2071                         return -EINVAL;
2072                 ref = find_ref_lock(hdl, id);
2073                 if (ref == NULL)
2074                         return -EINVAL;
2075                 ctrl = ref->ctrl;
2076                 if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
2077                         return -EINVAL;
2078
2079                 if (ctrl->cluster[0]->ncontrols > 1)
2080                         have_clusters = true;
2081                 if (ctrl->cluster[0] != ctrl)
2082                         ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
2083                 /* Store the ref to the master control of the cluster */
2084                 h->mref = ref;
2085                 h->ctrl = ctrl;
2086                 /* Initially set next to 0, meaning that there is no other
2087                    control in this helper array belonging to the same
2088                    cluster */
2089                 h->next = 0;
2090         }
2091
2092         /* We are done if there were no controls that belong to a multi-
2093            control cluster. */
2094         if (!have_clusters)
2095                 return 0;
2096
2097         /* The code below figures out in O(n) time which controls in the list
2098            belong to the same cluster. */
2099
2100         /* This has to be done with the handler lock taken. */
2101         mutex_lock(hdl->lock);
2102
2103         /* First zero the helper field in the master control references */
2104         for (i = 0; i < cs->count; i++)
2105                 helpers[i].mref->helper = 0;
2106         for (i = 0, h = helpers; i < cs->count; i++, h++) {
2107                 struct v4l2_ctrl_ref *mref = h->mref;
2108
2109                 /* If the mref->helper is set, then it points to an earlier
2110                    helper that belongs to the same cluster. */
2111                 if (mref->helper) {
2112                         /* Set the next field of mref->helper to the current
2113                            index: this means that that earlier helper now
2114                            points to the next helper in the same cluster. */
2115                         mref->helper->next = i;
2116                         /* mref should be set only for the first helper in the
2117                            cluster, clear the others. */
2118                         h->mref = NULL;
2119                 }
2120                 /* Point the mref helper to the current helper struct. */
2121                 mref->helper = h;
2122         }
2123         mutex_unlock(hdl->lock);
2124         return 0;
2125 }
2126
2127 /* Handles the corner case where cs->count == 0. It checks whether the
2128    specified control class exists. If that class ID is 0, then it checks
2129    whether there are any controls at all. */
2130 static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
2131 {
2132         if (ctrl_class == 0)
2133                 return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
2134         return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
2135 }
2136
2137
2138
2139 /* Get extended controls. Allocates the helpers array if needed. */
2140 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2141 {
2142         struct v4l2_ctrl_helper helper[4];
2143         struct v4l2_ctrl_helper *helpers = helper;
2144         int ret;
2145         int i, j;
2146
2147         cs->error_idx = cs->count;
2148         cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2149
2150         if (hdl == NULL)
2151                 return -EINVAL;
2152
2153         if (cs->count == 0)
2154                 return class_check(hdl, cs->ctrl_class);
2155
2156         if (cs->count > ARRAY_SIZE(helper)) {
2157                 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2158                                         GFP_KERNEL);
2159                 if (helpers == NULL)
2160                         return -ENOMEM;
2161         }
2162
2163         ret = prepare_ext_ctrls(hdl, cs, helpers);
2164         cs->error_idx = cs->count;
2165
2166         for (i = 0; !ret && i < cs->count; i++)
2167                 if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2168                         ret = -EACCES;
2169
2170         for (i = 0; !ret && i < cs->count; i++) {
2171                 int (*ctrl_to_user)(struct v4l2_ext_control *c,
2172                                     struct v4l2_ctrl *ctrl) = cur_to_user;
2173                 struct v4l2_ctrl *master;
2174
2175                 if (helpers[i].mref == NULL)
2176                         continue;
2177
2178                 master = helpers[i].mref->ctrl;
2179                 cs->error_idx = i;
2180
2181                 v4l2_ctrl_lock(master);
2182
2183                 /* g_volatile_ctrl will update the new control values */
2184                 if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
2185                         (master->has_volatiles && !is_cur_manual(master))) {
2186                         for (j = 0; j < master->ncontrols; j++)
2187                                 cur_to_new(master->cluster[j]);
2188                         ret = call_op(master, g_volatile_ctrl);
2189                         ctrl_to_user = new_to_user;
2190                 }
2191                 /* If OK, then copy the current (for non-volatile controls)
2192                    or the new (for volatile controls) control values to the
2193                    caller */
2194                 if (!ret) {
2195                         u32 idx = i;
2196
2197                         do {
2198                                 ret = ctrl_to_user(cs->controls + idx,
2199                                                    helpers[idx].ctrl);
2200                                 idx = helpers[idx].next;
2201                         } while (!ret && idx);
2202                 }
2203                 v4l2_ctrl_unlock(master);
2204         }
2205
2206         if (cs->count > ARRAY_SIZE(helper))
2207                 kfree(helpers);
2208         return ret;
2209 }
2210 EXPORT_SYMBOL(v4l2_g_ext_ctrls);
2211
2212 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2213 {
2214         return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
2215 }
2216 EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
2217
2218 /* Helper function to get a single control */
2219 static int get_ctrl(struct v4l2_ctrl *ctrl, s32 *val)
2220 {
2221         struct v4l2_ctrl *master = ctrl->cluster[0];
2222         int ret = 0;
2223         int i;
2224
2225         if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
2226                 return -EACCES;
2227
2228         v4l2_ctrl_lock(master);
2229         /* g_volatile_ctrl will update the current control values */
2230         if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
2231                 for (i = 0; i < master->ncontrols; i++)
2232                         cur_to_new(master->cluster[i]);
2233                 ret = call_op(master, g_volatile_ctrl);
2234                 *val = ctrl->val;
2235         } else {
2236                 *val = ctrl->cur.val;
2237         }
2238         v4l2_ctrl_unlock(master);
2239         return ret;
2240 }
2241
2242 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
2243 {
2244         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2245
2246         if (ctrl == NULL || !type_is_int(ctrl))
2247                 return -EINVAL;
2248         return get_ctrl(ctrl, &control->value);
2249 }
2250 EXPORT_SYMBOL(v4l2_g_ctrl);
2251
2252 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2253 {
2254         return v4l2_g_ctrl(sd->ctrl_handler, control);
2255 }
2256 EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
2257
2258 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
2259 {
2260         s32 val = 0;
2261
2262         /* It's a driver bug if this happens. */
2263         WARN_ON(!type_is_int(ctrl));
2264         get_ctrl(ctrl, &val);
2265         return val;
2266 }
2267 EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
2268
2269
2270 /* Core function that calls try/s_ctrl and ensures that the new value is
2271    copied to the current value on a set.
2272    Must be called with ctrl->handler->lock held. */
2273 static int try_or_set_cluster(struct v4l2_fh *fh,
2274                               struct v4l2_ctrl *master, bool set)
2275 {
2276         bool update_flag;
2277         int ret;
2278         int i;
2279
2280         /* Go through the cluster and either validate the new value or
2281            (if no new value was set), copy the current value to the new
2282            value, ensuring a consistent view for the control ops when
2283            called. */
2284         for (i = 0; i < master->ncontrols; i++) {
2285                 struct v4l2_ctrl *ctrl = master->cluster[i];
2286
2287                 if (ctrl == NULL)
2288                         continue;
2289
2290                 if (!ctrl->is_new) {
2291                         cur_to_new(ctrl);
2292                         continue;
2293                 }
2294                 /* Check again: it may have changed since the
2295                    previous check in try_or_set_ext_ctrls(). */
2296                 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2297                         return -EBUSY;
2298         }
2299
2300         ret = call_op(master, try_ctrl);
2301
2302         /* Don't set if there is no change */
2303         if (ret || !set || !cluster_changed(master))
2304                 return ret;
2305         ret = call_op(master, s_ctrl);
2306         if (ret)
2307                 return ret;
2308
2309         /* If OK, then make the new values permanent. */
2310         update_flag = is_cur_manual(master) != is_new_manual(master);
2311         for (i = 0; i < master->ncontrols; i++)
2312                 new_to_cur(fh, master->cluster[i], update_flag && i > 0);
2313         return 0;
2314 }
2315
2316 /* Validate controls. */
2317 static int validate_ctrls(struct v4l2_ext_controls *cs,
2318                           struct v4l2_ctrl_helper *helpers, bool set)
2319 {
2320         unsigned i;
2321         int ret = 0;
2322
2323         cs->error_idx = cs->count;
2324         for (i = 0; i < cs->count; i++) {
2325                 struct v4l2_ctrl *ctrl = helpers[i].ctrl;
2326
2327                 cs->error_idx = i;
2328
2329                 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2330                         return -EACCES;
2331                 /* This test is also done in try_set_control_cluster() which
2332                    is called in atomic context, so that has the final say,
2333                    but it makes sense to do an up-front check as well. Once
2334                    an error occurs in try_set_control_cluster() some other
2335                    controls may have been set already and we want to do a
2336                    best-effort to avoid that. */
2337                 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
2338                         return -EBUSY;
2339                 ret = validate_new(ctrl, &cs->controls[i]);
2340                 if (ret)
2341                         return ret;
2342         }
2343         return 0;
2344 }
2345
2346 /* Obtain the current volatile values of an autocluster and mark them
2347    as new. */
2348 static void update_from_auto_cluster(struct v4l2_ctrl *master)
2349 {
2350         int i;
2351
2352         for (i = 0; i < master->ncontrols; i++)
2353                 cur_to_new(master->cluster[i]);
2354         if (!call_op(master, g_volatile_ctrl))
2355                 for (i = 1; i < master->ncontrols; i++)
2356                         if (master->cluster[i])
2357                                 master->cluster[i]->is_new = 1;
2358 }
2359
2360 /* Try or try-and-set controls */
2361 static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2362                              struct v4l2_ext_controls *cs,
2363                              bool set)
2364 {
2365         struct v4l2_ctrl_helper helper[4];
2366         struct v4l2_ctrl_helper *helpers = helper;
2367         unsigned i, j;
2368         int ret;
2369
2370         cs->error_idx = cs->count;
2371         cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
2372
2373         if (hdl == NULL)
2374                 return -EINVAL;
2375
2376         if (cs->count == 0)
2377                 return class_check(hdl, cs->ctrl_class);
2378
2379         if (cs->count > ARRAY_SIZE(helper)) {
2380                 helpers = kmalloc_array(cs->count, sizeof(helper[0]),
2381                                         GFP_KERNEL);
2382                 if (!helpers)
2383                         return -ENOMEM;
2384         }
2385         ret = prepare_ext_ctrls(hdl, cs, helpers);
2386         if (!ret)
2387                 ret = validate_ctrls(cs, helpers, set);
2388         if (ret && set)
2389                 cs->error_idx = cs->count;
2390         for (i = 0; !ret && i < cs->count; i++) {
2391                 struct v4l2_ctrl *master;
2392                 u32 idx = i;
2393
2394                 if (helpers[i].mref == NULL)
2395                         continue;
2396
2397                 cs->error_idx = i;
2398                 master = helpers[i].mref->ctrl;
2399                 v4l2_ctrl_lock(master);
2400
2401                 /* Reset the 'is_new' flags of the cluster */
2402                 for (j = 0; j < master->ncontrols; j++)
2403                         if (master->cluster[j])
2404                                 master->cluster[j]->is_new = 0;
2405
2406                 /* For volatile autoclusters that are currently in auto mode
2407                    we need to discover if it will be set to manual mode.
2408                    If so, then we have to copy the current volatile values
2409                    first since those will become the new manual values (which
2410                    may be overwritten by explicit new values from this set
2411                    of controls). */
2412                 if (master->is_auto && master->has_volatiles &&
2413                                                 !is_cur_manual(master)) {
2414                         /* Pick an initial non-manual value */
2415                         s32 new_auto_val = master->manual_mode_value + 1;
2416                         u32 tmp_idx = idx;
2417
2418                         do {
2419                                 /* Check if the auto control is part of the
2420                                    list, and remember the new value. */
2421                                 if (helpers[tmp_idx].ctrl == master)
2422                                         new_auto_val = cs->controls[tmp_idx].value;
2423                                 tmp_idx = helpers[tmp_idx].next;
2424                         } while (tmp_idx);
2425                         /* If the new value == the manual value, then copy
2426                            the current volatile values. */
2427                         if (new_auto_val == master->manual_mode_value)
2428                                 update_from_auto_cluster(master);
2429                 }
2430
2431                 /* Copy the new caller-supplied control values.
2432                    user_to_new() sets 'is_new' to 1. */
2433                 do {
2434                         ret = user_to_new(cs->controls + idx, helpers[idx].ctrl);
2435                         idx = helpers[idx].next;
2436                 } while (!ret && idx);
2437
2438                 if (!ret)
2439                         ret = try_or_set_cluster(fh, master, set);
2440
2441                 /* Copy the new values back to userspace. */
2442                 if (!ret) {
2443                         idx = i;
2444                         do {
2445                                 ret = new_to_user(cs->controls + idx,
2446                                                 helpers[idx].ctrl);
2447                                 idx = helpers[idx].next;
2448                         } while (!ret && idx);
2449                 }
2450                 v4l2_ctrl_unlock(master);
2451         }
2452
2453         if (cs->count > ARRAY_SIZE(helper))
2454                 kfree(helpers);
2455         return ret;
2456 }
2457
2458 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
2459 {
2460         return try_set_ext_ctrls(NULL, hdl, cs, false);
2461 }
2462 EXPORT_SYMBOL(v4l2_try_ext_ctrls);
2463
2464 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2465                                         struct v4l2_ext_controls *cs)
2466 {
2467         return try_set_ext_ctrls(fh, hdl, cs, true);
2468 }
2469 EXPORT_SYMBOL(v4l2_s_ext_ctrls);
2470
2471 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2472 {
2473         return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
2474 }
2475 EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
2476
2477 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2478 {
2479         return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
2480 }
2481 EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
2482
2483 /* Helper function for VIDIOC_S_CTRL compatibility */
2484 static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, s32 *val)
2485 {
2486         struct v4l2_ctrl *master = ctrl->cluster[0];
2487         int ret;
2488         int i;
2489
2490         ret = validate_new_int(ctrl, val);
2491         if (ret)
2492                 return ret;
2493
2494         v4l2_ctrl_lock(ctrl);
2495
2496         /* Reset the 'is_new' flags of the cluster */
2497         for (i = 0; i < master->ncontrols; i++)
2498                 if (master->cluster[i])
2499                         master->cluster[i]->is_new = 0;
2500
2501         /* For autoclusters with volatiles that are switched from auto to
2502            manual mode we have to update the current volatile values since
2503            those will become the initial manual values after such a switch. */
2504         if (master->is_auto && master->has_volatiles && ctrl == master &&
2505             !is_cur_manual(master) && *val == master->manual_mode_value)
2506                 update_from_auto_cluster(master);
2507         ctrl->val = *val;
2508         ctrl->is_new = 1;
2509         ret = try_or_set_cluster(fh, master, true);
2510         *val = ctrl->cur.val;
2511         v4l2_ctrl_unlock(ctrl);
2512         return ret;
2513 }
2514
2515 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
2516                                         struct v4l2_control *control)
2517 {
2518         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
2519
2520         if (ctrl == NULL || !type_is_int(ctrl))
2521                 return -EINVAL;
2522
2523         if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
2524                 return -EACCES;
2525
2526         return set_ctrl(fh, ctrl, &control->value);
2527 }
2528 EXPORT_SYMBOL(v4l2_s_ctrl);
2529
2530 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2531 {
2532         return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
2533 }
2534 EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
2535
2536 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
2537 {
2538         /* It's a driver bug if this happens. */
2539         WARN_ON(!type_is_int(ctrl));
2540         return set_ctrl(NULL, ctrl, &val);
2541 }
2542 EXPORT_SYMBOL(v4l2_ctrl_s_ctrl);
2543
2544 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev)
2545 {
2546         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2547
2548         if (ctrl == NULL)
2549                 return -EINVAL;
2550
2551         v4l2_ctrl_lock(ctrl);
2552         list_add_tail(&sev->node, &ctrl->ev_subs);
2553         if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
2554             (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
2555                 struct v4l2_event ev;
2556                 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
2557
2558                 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
2559                         changes |= V4L2_EVENT_CTRL_CH_VALUE;
2560                 fill_event(&ev, ctrl, changes);
2561                 v4l2_event_queue_fh(sev->fh, &ev);
2562         }
2563         v4l2_ctrl_unlock(ctrl);
2564         return 0;
2565 }
2566
2567 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
2568 {
2569         struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
2570
2571         v4l2_ctrl_lock(ctrl);
2572         list_del(&sev->node);
2573         v4l2_ctrl_unlock(ctrl);
2574 }
2575
2576 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
2577 {
2578         u32 old_changes = old->u.ctrl.changes;
2579
2580         old->u.ctrl = new->u.ctrl;
2581         old->u.ctrl.changes |= old_changes;
2582 }
2583 EXPORT_SYMBOL(v4l2_ctrl_replace);
2584
2585 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
2586 {
2587         new->u.ctrl.changes |= old->u.ctrl.changes;
2588 }
2589 EXPORT_SYMBOL(v4l2_ctrl_merge);
2590
2591 const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
2592         .add = v4l2_ctrl_add_event,
2593         .del = v4l2_ctrl_del_event,
2594         .replace = v4l2_ctrl_replace,
2595         .merge = v4l2_ctrl_merge,
2596 };
2597 EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
2598
2599 int v4l2_ctrl_log_status(struct file *file, void *fh)
2600 {
2601         struct video_device *vfd = video_devdata(file);
2602         struct v4l2_fh *vfh = file->private_data;
2603
2604         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
2605                 v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
2606                         vfd->v4l2_dev->name);
2607         return 0;
2608 }
2609 EXPORT_SYMBOL(v4l2_ctrl_log_status);
2610
2611 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
2612                                 struct v4l2_event_subscription *sub)
2613 {
2614         if (sub->type == V4L2_EVENT_CTRL)
2615                 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
2616         return -EINVAL;
2617 }
2618 EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
2619
2620 unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
2621 {
2622         struct v4l2_fh *fh = file->private_data;
2623
2624         if (v4l2_event_pending(fh))
2625                 return POLLPRI;
2626         poll_wait(file, &fh->wait, wait);
2627         return 0;
2628 }
2629 EXPORT_SYMBOL(v4l2_ctrl_poll);