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