blob: 0a96cc35738c4e38574542d039252c41bbc08dd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Video for Linux Two
3 *
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
6 *
7 * This file replaces the videodev.c file that comes with the
8 * regular kernel distribution.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
Mauro Carvalho Chehab43db48d2007-01-09 11:20:59 -030015 * Author: Bill Dirks <bill@thedirks.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * based on code by Alan Cox, <alan@cymru.net>
17 *
18 */
19
20/*
21 * Video capture interface for Linux
22 *
23 * A generic video device interface for the LINUX operating system
24 * using a set of device structures/vectors for low level operations.
25 *
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
30 *
31 * Author: Alan Cox, <alan@redhat.com>
32 *
33 * Fixes:
34 */
35
36/*
37 * Video4linux 1/2 integration by Justin Schoeman
38 * <justin@suntiger.ee.up.ac.za>
39 * 2.4 PROCFS support ported from 2.4 kernels by
Jan Engelhardt96de0e22007-10-19 23:21:04 +020040 * Iñaki García Etxebarria <garetxe@euskalnet.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Makefile fix by "W. Michael Petullo" <mike@flyn.org>
42 * 2.4 devfs support ported from 2.4 kernels by
43 * Dan Merillat <dan@merillat.org>
44 * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman)
45 */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
51#include <linux/string.h>
52#include <linux/errno.h>
Hans Verkuilf3d092b2007-02-23 20:55:14 -030053#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
55#include <asm/system.h>
56#include <asm/pgtable.h>
57#include <asm/io.h>
58#include <asm/div64.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030059#define __OLD_VIDIOC_ /* To allow fixing old calls*/
Michael Krufky5e453dc2006-01-09 15:32:31 -020060#include <media/v4l2-common.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030061#include <media/v4l2-chip-ident.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#ifdef CONFIG_KMOD
64#include <linux/kmod.h>
65#endif
66
Hans Verkuil33b687c2008-07-25 05:32:50 -030067#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
70MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
71MODULE_LICENSE("GPL");
72
73/*
74 *
75 * V 4 L 2 D R I V E R H E L P E R A P I
76 *
77 */
78
79/*
80 * Video Standard Operations (contributed by Michael Schimek)
81 */
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* ----------------------------------------------------------------- */
85/* priority handling */
86
87#define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND || \
88 val == V4L2_PRIORITY_INTERACTIVE || \
89 val == V4L2_PRIORITY_RECORD)
90
91int v4l2_prio_init(struct v4l2_prio_state *global)
92{
93 memset(global,0,sizeof(*global));
94 return 0;
95}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -030096EXPORT_SYMBOL(v4l2_prio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
99 enum v4l2_priority new)
100{
101 if (!V4L2_PRIO_VALID(new))
102 return -EINVAL;
103 if (*local == new)
104 return 0;
105
106 atomic_inc(&global->prios[new]);
107 if (V4L2_PRIO_VALID(*local))
108 atomic_dec(&global->prios[*local]);
109 *local = new;
110 return 0;
111}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300112EXPORT_SYMBOL(v4l2_prio_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
115{
116 return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
117}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300118EXPORT_SYMBOL(v4l2_prio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
121{
122 if (V4L2_PRIO_VALID(*local))
123 atomic_dec(&global->prios[*local]);
124 return 0;
125}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300126EXPORT_SYMBOL(v4l2_prio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
129{
130 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
131 return V4L2_PRIORITY_RECORD;
132 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
133 return V4L2_PRIORITY_INTERACTIVE;
134 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
135 return V4L2_PRIORITY_BACKGROUND;
136 return V4L2_PRIORITY_UNSET;
137}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300138EXPORT_SYMBOL(v4l2_prio_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
141{
142 if (*local < v4l2_prio_max(global))
143 return -EBUSY;
144 return 0;
145}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300146EXPORT_SYMBOL(v4l2_prio_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* ----------------------------------------------------------------- */
149
Hans Verkuil9cb23182006-06-18 14:11:08 -0300150/* Helper functions for control handling */
151
152/* Check for correctness of the ctrl's value based on the data from
153 struct v4l2_queryctrl and the available menu items. Note that
154 menu_items may be NULL, in that case it is ignored. */
155int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
156 const char **menu_items)
157{
158 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
159 return -EINVAL;
160 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
161 return -EBUSY;
162 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
163 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
164 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
165 return 0;
166 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
167 return -ERANGE;
168 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
169 if (menu_items[ctrl->value] == NULL ||
170 menu_items[ctrl->value][0] == '\0')
171 return -EINVAL;
172 }
173 return 0;
174}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300175EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300176
177/* Returns NULL or a character pointer array containing the menu for
178 the given control ID. The pointer array ends with a NULL pointer.
179 An empty string signifies a menu entry that is invalid. This allows
180 drivers to disable certain options if it is not supported. */
181const char **v4l2_ctrl_get_menu(u32 id)
182{
183 static const char *mpeg_audio_sampling_freq[] = {
184 "44.1 kHz",
185 "48 kHz",
186 "32 kHz",
187 NULL
188 };
189 static const char *mpeg_audio_encoding[] = {
Hans Verkuile6b5da82008-08-08 07:38:07 -0300190 "MPEG-1/2 Layer I",
191 "MPEG-1/2 Layer II",
192 "MPEG-1/2 Layer III",
193 "MPEG-2/4 AAC",
194 "AC-3",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300195 NULL
196 };
197 static const char *mpeg_audio_l1_bitrate[] = {
198 "32 kbps",
199 "64 kbps",
200 "96 kbps",
201 "128 kbps",
202 "160 kbps",
203 "192 kbps",
204 "224 kbps",
205 "256 kbps",
206 "288 kbps",
207 "320 kbps",
208 "352 kbps",
209 "384 kbps",
210 "416 kbps",
211 "448 kbps",
212 NULL
213 };
214 static const char *mpeg_audio_l2_bitrate[] = {
215 "32 kbps",
216 "48 kbps",
217 "56 kbps",
218 "64 kbps",
219 "80 kbps",
220 "96 kbps",
221 "112 kbps",
222 "128 kbps",
223 "160 kbps",
224 "192 kbps",
225 "224 kbps",
226 "256 kbps",
227 "320 kbps",
228 "384 kbps",
229 NULL
230 };
231 static const char *mpeg_audio_l3_bitrate[] = {
232 "32 kbps",
233 "40 kbps",
234 "48 kbps",
235 "56 kbps",
236 "64 kbps",
237 "80 kbps",
238 "96 kbps",
239 "112 kbps",
240 "128 kbps",
241 "160 kbps",
242 "192 kbps",
243 "224 kbps",
244 "256 kbps",
245 "320 kbps",
246 NULL
247 };
Hans Verkuile6b5da82008-08-08 07:38:07 -0300248 static const char *mpeg_audio_ac3_bitrate[] = {
249 "32 kbps",
250 "40 kbps",
251 "48 kbps",
252 "56 kbps",
253 "64 kbps",
254 "80 kbps",
255 "96 kbps",
256 "112 kbps",
257 "128 kbps",
258 "160 kbps",
259 "192 kbps",
260 "224 kbps",
261 "256 kbps",
262 "320 kbps",
263 "384 kbps",
264 "448 kbps",
265 "512 kbps",
266 "576 kbps",
267 "640 kbps",
268 NULL
269 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300270 static const char *mpeg_audio_mode[] = {
271 "Stereo",
272 "Joint Stereo",
273 "Dual",
274 "Mono",
275 NULL
276 };
277 static const char *mpeg_audio_mode_extension[] = {
278 "Bound 4",
279 "Bound 8",
280 "Bound 12",
281 "Bound 16",
282 NULL
283 };
284 static const char *mpeg_audio_emphasis[] = {
285 "No Emphasis",
286 "50/15 us",
287 "CCITT J17",
288 NULL
289 };
290 static const char *mpeg_audio_crc[] = {
291 "No CRC",
292 "16-bit CRC",
293 NULL
294 };
295 static const char *mpeg_video_encoding[] = {
296 "MPEG-1",
297 "MPEG-2",
Janne Grunau188919a2008-08-08 07:21:00 -0300298 "MPEG-4 AVC",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300299 NULL
300 };
301 static const char *mpeg_video_aspect[] = {
302 "1x1",
303 "4x3",
304 "16x9",
305 "2.21x1",
306 NULL
307 };
308 static const char *mpeg_video_bitrate_mode[] = {
309 "Variable Bitrate",
310 "Constant Bitrate",
311 NULL
312 };
313 static const char *mpeg_stream_type[] = {
314 "MPEG-2 Program Stream",
315 "MPEG-2 Transport Stream",
316 "MPEG-1 System Stream",
317 "MPEG-2 DVD-compatible Stream",
318 "MPEG-1 VCD-compatible Stream",
319 "MPEG-2 SVCD-compatible Stream",
320 NULL
321 };
Hans Verkuil8cbde942006-06-24 14:36:02 -0300322 static const char *mpeg_stream_vbi_fmt[] = {
323 "No VBI",
Hans Verkuil99523512006-06-25 11:18:54 -0300324 "Private packet, IVTV format",
Hans Verkuil8cbde942006-06-24 14:36:02 -0300325 NULL
326 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300327
328 switch (id) {
329 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
330 return mpeg_audio_sampling_freq;
331 case V4L2_CID_MPEG_AUDIO_ENCODING:
332 return mpeg_audio_encoding;
333 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
334 return mpeg_audio_l1_bitrate;
335 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
336 return mpeg_audio_l2_bitrate;
337 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
338 return mpeg_audio_l3_bitrate;
Hans Verkuile6b5da82008-08-08 07:38:07 -0300339 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
340 return mpeg_audio_ac3_bitrate;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300341 case V4L2_CID_MPEG_AUDIO_MODE:
342 return mpeg_audio_mode;
343 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
344 return mpeg_audio_mode_extension;
345 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
346 return mpeg_audio_emphasis;
347 case V4L2_CID_MPEG_AUDIO_CRC:
348 return mpeg_audio_crc;
349 case V4L2_CID_MPEG_VIDEO_ENCODING:
350 return mpeg_video_encoding;
351 case V4L2_CID_MPEG_VIDEO_ASPECT:
352 return mpeg_video_aspect;
353 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
354 return mpeg_video_bitrate_mode;
355 case V4L2_CID_MPEG_STREAM_TYPE:
356 return mpeg_stream_type;
Hans Verkuil8cbde942006-06-24 14:36:02 -0300357 case V4L2_CID_MPEG_STREAM_VBI_FMT:
358 return mpeg_stream_vbi_fmt;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300359 default:
360 return NULL;
361 }
362}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300363EXPORT_SYMBOL(v4l2_ctrl_get_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300364
365/* Fill in a struct v4l2_queryctrl */
366int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
367{
368 const char *name;
369
370 qctrl->flags = 0;
371 switch (qctrl->id) {
372 /* USER controls */
373 case V4L2_CID_USER_CLASS: name = "User Controls"; break;
374 case V4L2_CID_AUDIO_VOLUME: name = "Volume"; break;
375 case V4L2_CID_AUDIO_MUTE: name = "Mute"; break;
376 case V4L2_CID_AUDIO_BALANCE: name = "Balance"; break;
377 case V4L2_CID_AUDIO_BASS: name = "Bass"; break;
378 case V4L2_CID_AUDIO_TREBLE: name = "Treble"; break;
379 case V4L2_CID_AUDIO_LOUDNESS: name = "Loudness"; break;
380 case V4L2_CID_BRIGHTNESS: name = "Brightness"; break;
381 case V4L2_CID_CONTRAST: name = "Contrast"; break;
382 case V4L2_CID_SATURATION: name = "Saturation"; break;
383 case V4L2_CID_HUE: name = "Hue"; break;
384
385 /* MPEG controls */
386 case V4L2_CID_MPEG_CLASS: name = "MPEG Encoder Controls"; break;
387 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: name = "Audio Sampling Frequency"; break;
Janne Grunau188919a2008-08-08 07:21:00 -0300388 case V4L2_CID_MPEG_AUDIO_ENCODING: name = "Audio Encoding"; break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300389 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: name = "Audio Layer I Bitrate"; break;
390 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: name = "Audio Layer II Bitrate"; break;
391 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: name = "Audio Layer III Bitrate"; break;
Hans Verkuile6b5da82008-08-08 07:38:07 -0300392 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: name = "Audio AC-3 Bitrate"; break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300393 case V4L2_CID_MPEG_AUDIO_MODE: name = "Audio Stereo Mode"; break;
394 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: name = "Audio Stereo Mode Extension"; break;
395 case V4L2_CID_MPEG_AUDIO_EMPHASIS: name = "Audio Emphasis"; break;
396 case V4L2_CID_MPEG_AUDIO_CRC: name = "Audio CRC"; break;
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300397 case V4L2_CID_MPEG_AUDIO_MUTE: name = "Audio Mute"; break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300398 case V4L2_CID_MPEG_VIDEO_ENCODING: name = "Video Encoding"; break;
399 case V4L2_CID_MPEG_VIDEO_ASPECT: name = "Video Aspect"; break;
400 case V4L2_CID_MPEG_VIDEO_B_FRAMES: name = "Video B Frames"; break;
401 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: name = "Video GOP Size"; break;
402 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: name = "Video GOP Closure"; break;
403 case V4L2_CID_MPEG_VIDEO_PULLDOWN: name = "Video Pulldown"; break;
404 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: name = "Video Bitrate Mode"; break;
405 case V4L2_CID_MPEG_VIDEO_BITRATE: name = "Video Bitrate"; break;
406 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: name = "Video Peak Bitrate"; break;
407 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: name = "Video Temporal Decimation"; break;
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300408 case V4L2_CID_MPEG_VIDEO_MUTE: name = "Video Mute"; break;
409 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: name = "Video Mute YUV"; break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300410 case V4L2_CID_MPEG_STREAM_TYPE: name = "Stream Type"; break;
411 case V4L2_CID_MPEG_STREAM_PID_PMT: name = "Stream PMT Program ID"; break;
412 case V4L2_CID_MPEG_STREAM_PID_AUDIO: name = "Stream Audio Program ID"; break;
413 case V4L2_CID_MPEG_STREAM_PID_VIDEO: name = "Stream Video Program ID"; break;
414 case V4L2_CID_MPEG_STREAM_PID_PCR: name = "Stream PCR Program ID"; break;
415 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: name = "Stream PES Audio ID"; break;
416 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: name = "Stream PES Video ID"; break;
Hans Verkuil8cbde942006-06-24 14:36:02 -0300417 case V4L2_CID_MPEG_STREAM_VBI_FMT: name = "Stream VBI Format"; break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300418
419 default:
420 return -EINVAL;
421 }
422 switch (qctrl->id) {
423 case V4L2_CID_AUDIO_MUTE:
424 case V4L2_CID_AUDIO_LOUDNESS:
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300425 case V4L2_CID_MPEG_AUDIO_MUTE:
Hans Verkuil01f1e442007-08-21 18:32:42 -0300426 case V4L2_CID_MPEG_VIDEO_MUTE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300427 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
428 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
429 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
430 min = 0;
431 max = step = 1;
432 break;
433 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
434 case V4L2_CID_MPEG_AUDIO_ENCODING:
435 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
436 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
437 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
Hans Verkuile6b5da82008-08-08 07:38:07 -0300438 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300439 case V4L2_CID_MPEG_AUDIO_MODE:
440 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
441 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
442 case V4L2_CID_MPEG_AUDIO_CRC:
443 case V4L2_CID_MPEG_VIDEO_ENCODING:
444 case V4L2_CID_MPEG_VIDEO_ASPECT:
445 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
446 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil8cbde942006-06-24 14:36:02 -0300447 case V4L2_CID_MPEG_STREAM_VBI_FMT:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300448 qctrl->type = V4L2_CTRL_TYPE_MENU;
449 step = 1;
450 break;
451 case V4L2_CID_USER_CLASS:
452 case V4L2_CID_MPEG_CLASS:
453 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
454 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
455 min = max = step = def = 0;
456 break;
457 default:
458 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
459 break;
460 }
461 switch (qctrl->id) {
462 case V4L2_CID_MPEG_AUDIO_ENCODING:
463 case V4L2_CID_MPEG_AUDIO_MODE:
464 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
465 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
466 case V4L2_CID_MPEG_STREAM_TYPE:
467 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
468 break;
469 case V4L2_CID_AUDIO_VOLUME:
470 case V4L2_CID_AUDIO_BALANCE:
471 case V4L2_CID_AUDIO_BASS:
472 case V4L2_CID_AUDIO_TREBLE:
473 case V4L2_CID_BRIGHTNESS:
474 case V4L2_CID_CONTRAST:
475 case V4L2_CID_SATURATION:
476 case V4L2_CID_HUE:
477 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
478 break;
479 }
480 qctrl->minimum = min;
481 qctrl->maximum = max;
482 qctrl->step = step;
483 qctrl->default_value = def;
484 qctrl->reserved[0] = qctrl->reserved[1] = 0;
485 snprintf(qctrl->name, sizeof(qctrl->name), name);
486 return 0;
487}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300488EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300489
490/* Fill in a struct v4l2_queryctrl with standard values based on
491 the control ID. */
492int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
493{
494 switch (qctrl->id) {
495 /* USER controls */
496 case V4L2_CID_USER_CLASS:
497 case V4L2_CID_MPEG_CLASS:
498 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
499 case V4L2_CID_AUDIO_VOLUME:
500 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
501 case V4L2_CID_AUDIO_MUTE:
502 case V4L2_CID_AUDIO_LOUDNESS:
503 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
504 case V4L2_CID_AUDIO_BALANCE:
505 case V4L2_CID_AUDIO_BASS:
506 case V4L2_CID_AUDIO_TREBLE:
507 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
508 case V4L2_CID_BRIGHTNESS:
509 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
510 case V4L2_CID_CONTRAST:
511 case V4L2_CID_SATURATION:
512 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
513 case V4L2_CID_HUE:
514 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
515
516 /* MPEG controls */
517 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
518 return v4l2_ctrl_query_fill(qctrl,
519 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
520 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
521 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
522 case V4L2_CID_MPEG_AUDIO_ENCODING:
523 return v4l2_ctrl_query_fill(qctrl,
524 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
Hans Verkuile6b5da82008-08-08 07:38:07 -0300525 V4L2_MPEG_AUDIO_ENCODING_AC3, 1,
Hans Verkuil9cb23182006-06-18 14:11:08 -0300526 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
527 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
528 return v4l2_ctrl_query_fill(qctrl,
529 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
530 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
531 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
532 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
533 return v4l2_ctrl_query_fill(qctrl,
534 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
535 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
536 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
537 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
538 return v4l2_ctrl_query_fill(qctrl,
539 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
540 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
541 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
Hans Verkuile6b5da82008-08-08 07:38:07 -0300542 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
543 return v4l2_ctrl_query_fill(qctrl,
544 V4L2_MPEG_AUDIO_AC3_BITRATE_32K,
545 V4L2_MPEG_AUDIO_AC3_BITRATE_640K, 1,
546 V4L2_MPEG_AUDIO_AC3_BITRATE_384K);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300547 case V4L2_CID_MPEG_AUDIO_MODE:
548 return v4l2_ctrl_query_fill(qctrl,
549 V4L2_MPEG_AUDIO_MODE_STEREO,
550 V4L2_MPEG_AUDIO_MODE_MONO, 1,
551 V4L2_MPEG_AUDIO_MODE_STEREO);
552 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
553 return v4l2_ctrl_query_fill(qctrl,
554 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
555 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
556 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
557 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
558 return v4l2_ctrl_query_fill(qctrl,
559 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
560 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
561 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
562 case V4L2_CID_MPEG_AUDIO_CRC:
563 return v4l2_ctrl_query_fill(qctrl,
564 V4L2_MPEG_AUDIO_CRC_NONE,
565 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
566 V4L2_MPEG_AUDIO_CRC_NONE);
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300567 case V4L2_CID_MPEG_AUDIO_MUTE:
568 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300569 case V4L2_CID_MPEG_VIDEO_ENCODING:
570 return v4l2_ctrl_query_fill(qctrl,
571 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
Janne Grunau188919a2008-08-08 07:21:00 -0300572 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
Hans Verkuil9cb23182006-06-18 14:11:08 -0300573 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
574 case V4L2_CID_MPEG_VIDEO_ASPECT:
575 return v4l2_ctrl_query_fill(qctrl,
576 V4L2_MPEG_VIDEO_ASPECT_1x1,
577 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
578 V4L2_MPEG_VIDEO_ASPECT_4x3);
579 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
580 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
581 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
582 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
583 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
584 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
585 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
586 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
587 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
588 return v4l2_ctrl_query_fill(qctrl,
589 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
590 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
591 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
592 case V4L2_CID_MPEG_VIDEO_BITRATE:
593 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
594 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
595 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
596 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
597 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300598 case V4L2_CID_MPEG_VIDEO_MUTE:
599 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
600 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: /* Init YUV (really YCbCr) to black */
601 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300602 case V4L2_CID_MPEG_STREAM_TYPE:
603 return v4l2_ctrl_query_fill(qctrl,
604 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
605 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
606 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
607 case V4L2_CID_MPEG_STREAM_PID_PMT:
608 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
609 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
610 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
611 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
612 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
613 case V4L2_CID_MPEG_STREAM_PID_PCR:
614 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
615 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
616 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
617 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
618 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
Hans Verkuil8cbde942006-06-24 14:36:02 -0300619 case V4L2_CID_MPEG_STREAM_VBI_FMT:
620 return v4l2_ctrl_query_fill(qctrl,
621 V4L2_MPEG_STREAM_VBI_FMT_NONE,
622 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
623 V4L2_MPEG_STREAM_VBI_FMT_NONE);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300624 default:
625 return -EINVAL;
626 }
627}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300628EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300629
630/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
631 the menu. The qctrl pointer may be NULL, in which case it is ignored. */
632int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
633 const char **menu_items)
634{
635 int i;
636
637 if (menu_items == NULL ||
638 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
639 return -EINVAL;
640 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
641 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
642 return -EINVAL;
643 snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
644 qmenu->reserved = 0;
645 return 0;
646}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300647EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300648
649/* ctrl_classes points to an array of u32 pointers, the last element is
650 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
651 Each array must be sorted low to high and belong to the same control
652 class. The array of u32 pointer must also be sorted, from low class IDs
653 to high class IDs.
654
655 This function returns the first ID that follows after the given ID.
656 When no more controls are available 0 is returned. */
657u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
658{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300659 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300660 const u32 *pctrl;
661
Hans Verkuil9cb23182006-06-18 14:11:08 -0300662 if (ctrl_classes == NULL)
663 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300664
665 /* if no query is desired, then check if the ID is part of ctrl_classes */
666 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
667 /* find class */
668 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
669 ctrl_classes++;
670 if (*ctrl_classes == NULL)
671 return 0;
672 pctrl = *ctrl_classes;
673 /* find control ID */
674 while (*pctrl && *pctrl != id) pctrl++;
675 return *pctrl ? id : 0;
676 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300677 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300678 id++; /* select next control */
679 /* find first class that matches (or is greater than) the class of
680 the ID */
681 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
682 ctrl_classes++;
683 /* no more classes */
684 if (*ctrl_classes == NULL)
685 return 0;
686 pctrl = *ctrl_classes;
687 /* find first ctrl within the class that is >= ID */
688 while (*pctrl && *pctrl < id) pctrl++;
689 if (*pctrl)
690 return *pctrl;
691 /* we are at the end of the controls of the current class. */
692 /* continue with next class if available */
693 ctrl_classes++;
694 if (*ctrl_classes == NULL)
695 return 0;
696 return **ctrl_classes;
697}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300698EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300699
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300700int v4l2_chip_match_host(u32 match_type, u32 match_chip)
701{
702 switch (match_type) {
703 case V4L2_CHIP_MATCH_HOST:
704 return match_chip == 0;
705 default:
706 return 0;
707 }
708}
709EXPORT_SYMBOL(v4l2_chip_match_host);
710
711#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300712int v4l2_chip_match_i2c_client(struct i2c_client *c, u32 match_type, u32 match_chip)
713{
714 switch (match_type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300715 case V4L2_CHIP_MATCH_I2C_DRIVER:
716 return (c != NULL && c->driver != NULL && c->driver->id == match_chip);
717 case V4L2_CHIP_MATCH_I2C_ADDR:
718 return (c != NULL && c->addr == match_chip);
719 default:
720 return 0;
721 }
722}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300723EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300724
Hans Verkuil3434eb72007-04-27 12:31:08 -0300725int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_chip_ident *chip,
726 u32 ident, u32 revision)
727{
728 if (!v4l2_chip_match_i2c_client(c, chip->match_type, chip->match_chip))
729 return 0;
730 if (chip->ident == V4L2_IDENT_NONE) {
731 chip->ident = ident;
732 chip->revision = revision;
733 }
734 else {
735 chip->ident = V4L2_IDENT_AMBIGUOUS;
736 chip->revision = 0;
737 }
738 return 0;
739}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300740EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300741
Hans Verkuil9cb23182006-06-18 14:11:08 -0300742/* ----------------------------------------------------------------- */
743
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300744/* Helper function for I2C legacy drivers */
745
746int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
Jean Delvared2653e92008-04-29 23:11:39 +0200747 const char *name,
748 int (*probe)(struct i2c_client *, const struct i2c_device_id *))
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300749{
750 struct i2c_client *client;
751 int err;
752
753 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Harvey Harrisona6a3a172008-04-28 16:50:03 -0700754 if (!client)
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300755 return -ENOMEM;
756
757 client->addr = address;
758 client->adapter = adapter;
759 client->driver = driver;
Hans Verkuil393bf552007-09-18 03:22:32 -0300760 strlcpy(client->name, name, sizeof(client->name));
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300761
Jean Delvared2653e92008-04-29 23:11:39 +0200762 err = probe(client, NULL);
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300763 if (err == 0) {
764 i2c_attach_client(client);
Hans Verkuil393bf552007-09-18 03:22:32 -0300765 } else {
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300766 kfree(client);
767 }
Hans Verkuil188f3452007-09-16 10:47:15 -0300768 return err != -ENOMEM ? 0 : err;
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300769}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300770EXPORT_SYMBOL(v4l2_i2c_attach);
771#endif