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