blob: 035c414507a138d6bbbc8181aef07b1f5e696ca1 [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 *
Alan Coxd9b01442008-10-27 15:13:47 -030031 * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
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>
Dmitri Belimov85e09212010-03-23 11:23:29 -030054#if defined(CONFIG_SPI)
55#include <linux/spi/spi.h>
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/uaccess.h>
58#include <asm/system.h>
59#include <asm/pgtable.h>
60#include <asm/io.h>
61#include <asm/div64.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030062#define __OLD_VIDIOC_ /* To allow fixing old calls*/
Michael Krufky5e453dc2006-01-09 15:32:31 -020063#include <media/v4l2-common.h>
Hans Verkuildd991202008-11-23 12:19:45 -030064#include <media/v4l2-device.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030065#include <media/v4l2-chip-ident.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300162 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
163 return 0;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300164 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
165 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
166 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
167 return 0;
168 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
169 return -ERANGE;
170 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
171 if (menu_items[ctrl->value] == NULL ||
172 menu_items[ctrl->value][0] == '\0')
173 return -EINVAL;
174 }
175 return 0;
176}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300177EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300178
179/* Returns NULL or a character pointer array containing the menu for
180 the given control ID. The pointer array ends with a NULL pointer.
181 An empty string signifies a menu entry that is invalid. This allows
182 drivers to disable certain options if it is not supported. */
183const char **v4l2_ctrl_get_menu(u32 id)
184{
185 static const char *mpeg_audio_sampling_freq[] = {
186 "44.1 kHz",
187 "48 kHz",
188 "32 kHz",
189 NULL
190 };
191 static const char *mpeg_audio_encoding[] = {
Hans Verkuile6b5da82008-08-08 07:38:07 -0300192 "MPEG-1/2 Layer I",
193 "MPEG-1/2 Layer II",
194 "MPEG-1/2 Layer III",
195 "MPEG-2/4 AAC",
196 "AC-3",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300197 NULL
198 };
199 static const char *mpeg_audio_l1_bitrate[] = {
200 "32 kbps",
201 "64 kbps",
202 "96 kbps",
203 "128 kbps",
204 "160 kbps",
205 "192 kbps",
206 "224 kbps",
207 "256 kbps",
208 "288 kbps",
209 "320 kbps",
210 "352 kbps",
211 "384 kbps",
212 "416 kbps",
213 "448 kbps",
214 NULL
215 };
216 static const char *mpeg_audio_l2_bitrate[] = {
217 "32 kbps",
218 "48 kbps",
219 "56 kbps",
220 "64 kbps",
221 "80 kbps",
222 "96 kbps",
223 "112 kbps",
224 "128 kbps",
225 "160 kbps",
226 "192 kbps",
227 "224 kbps",
228 "256 kbps",
229 "320 kbps",
230 "384 kbps",
231 NULL
232 };
233 static const char *mpeg_audio_l3_bitrate[] = {
234 "32 kbps",
235 "40 kbps",
236 "48 kbps",
237 "56 kbps",
238 "64 kbps",
239 "80 kbps",
240 "96 kbps",
241 "112 kbps",
242 "128 kbps",
243 "160 kbps",
244 "192 kbps",
245 "224 kbps",
246 "256 kbps",
247 "320 kbps",
248 NULL
249 };
Hans Verkuile6b5da82008-08-08 07:38:07 -0300250 static const char *mpeg_audio_ac3_bitrate[] = {
251 "32 kbps",
252 "40 kbps",
253 "48 kbps",
254 "56 kbps",
255 "64 kbps",
256 "80 kbps",
257 "96 kbps",
258 "112 kbps",
259 "128 kbps",
260 "160 kbps",
261 "192 kbps",
262 "224 kbps",
263 "256 kbps",
264 "320 kbps",
265 "384 kbps",
266 "448 kbps",
267 "512 kbps",
268 "576 kbps",
269 "640 kbps",
270 NULL
271 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300272 static const char *mpeg_audio_mode[] = {
273 "Stereo",
274 "Joint Stereo",
275 "Dual",
276 "Mono",
277 NULL
278 };
279 static const char *mpeg_audio_mode_extension[] = {
280 "Bound 4",
281 "Bound 8",
282 "Bound 12",
283 "Bound 16",
284 NULL
285 };
286 static const char *mpeg_audio_emphasis[] = {
287 "No Emphasis",
288 "50/15 us",
289 "CCITT J17",
290 NULL
291 };
292 static const char *mpeg_audio_crc[] = {
293 "No CRC",
294 "16-bit CRC",
295 NULL
296 };
297 static const char *mpeg_video_encoding[] = {
298 "MPEG-1",
299 "MPEG-2",
Janne Grunau188919a2008-08-08 07:21:00 -0300300 "MPEG-4 AVC",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300301 NULL
302 };
303 static const char *mpeg_video_aspect[] = {
304 "1x1",
305 "4x3",
306 "16x9",
307 "2.21x1",
308 NULL
309 };
310 static const char *mpeg_video_bitrate_mode[] = {
311 "Variable Bitrate",
312 "Constant Bitrate",
313 NULL
314 };
315 static const char *mpeg_stream_type[] = {
316 "MPEG-2 Program Stream",
317 "MPEG-2 Transport Stream",
318 "MPEG-1 System Stream",
319 "MPEG-2 DVD-compatible Stream",
320 "MPEG-1 VCD-compatible Stream",
321 "MPEG-2 SVCD-compatible Stream",
322 NULL
323 };
Hans Verkuil8cbde942006-06-24 14:36:02 -0300324 static const char *mpeg_stream_vbi_fmt[] = {
325 "No VBI",
Hans Verkuil99523512006-06-25 11:18:54 -0300326 "Private packet, IVTV format",
Hans Verkuil8cbde942006-06-24 14:36:02 -0300327 NULL
328 };
Laurent Pinchart74980152008-12-14 16:24:04 -0300329 static const char *camera_power_line_frequency[] = {
330 "Disabled",
331 "50 Hz",
332 "60 Hz",
333 NULL
334 };
335 static const char *camera_exposure_auto[] = {
336 "Auto Mode",
337 "Manual Mode",
338 "Shutter Priority Mode",
339 "Aperture Priority Mode",
340 NULL
341 };
Hans Verkuil11c469e2009-02-20 05:50:52 -0300342 static const char *colorfx[] = {
343 "None",
344 "Black & White",
345 "Sepia",
Xiaolin Zhang35e6aa92010-04-18 23:06:50 -0300346 "Negative",
347 "Emboss",
348 "Sketch",
349 "Sky blue",
350 "Grass green",
351 "Skin whiten",
352 "Vivid",
Hans Verkuil11c469e2009-02-20 05:50:52 -0300353 NULL
354 };
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300355 static const char *tune_preemphasis[] = {
356 "No preemphasis",
357 "50 useconds",
358 "75 useconds",
359 NULL,
360 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300361
362 switch (id) {
363 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
364 return mpeg_audio_sampling_freq;
365 case V4L2_CID_MPEG_AUDIO_ENCODING:
366 return mpeg_audio_encoding;
367 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
368 return mpeg_audio_l1_bitrate;
369 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
370 return mpeg_audio_l2_bitrate;
371 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
372 return mpeg_audio_l3_bitrate;
Hans Verkuile6b5da82008-08-08 07:38:07 -0300373 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
374 return mpeg_audio_ac3_bitrate;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300375 case V4L2_CID_MPEG_AUDIO_MODE:
376 return mpeg_audio_mode;
377 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
378 return mpeg_audio_mode_extension;
379 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
380 return mpeg_audio_emphasis;
381 case V4L2_CID_MPEG_AUDIO_CRC:
382 return mpeg_audio_crc;
383 case V4L2_CID_MPEG_VIDEO_ENCODING:
384 return mpeg_video_encoding;
385 case V4L2_CID_MPEG_VIDEO_ASPECT:
386 return mpeg_video_aspect;
387 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
388 return mpeg_video_bitrate_mode;
389 case V4L2_CID_MPEG_STREAM_TYPE:
390 return mpeg_stream_type;
Hans Verkuil8cbde942006-06-24 14:36:02 -0300391 case V4L2_CID_MPEG_STREAM_VBI_FMT:
392 return mpeg_stream_vbi_fmt;
Laurent Pinchart74980152008-12-14 16:24:04 -0300393 case V4L2_CID_POWER_LINE_FREQUENCY:
394 return camera_power_line_frequency;
395 case V4L2_CID_EXPOSURE_AUTO:
396 return camera_exposure_auto;
Hans Verkuil11c469e2009-02-20 05:50:52 -0300397 case V4L2_CID_COLORFX:
398 return colorfx;
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300399 case V4L2_CID_TUNE_PREEMPHASIS:
400 return tune_preemphasis;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300401 default:
402 return NULL;
403 }
404}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300405EXPORT_SYMBOL(v4l2_ctrl_get_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300406
Hans Verkuil69028d72008-08-08 07:55:00 -0300407/* Return the control name. */
408const char *v4l2_ctrl_get_name(u32 id)
409{
410 switch (id) {
411 /* USER controls */
Laurent Pinchart74980152008-12-14 16:24:04 -0300412 case V4L2_CID_USER_CLASS: return "User Controls";
Laurent Pinchart74980152008-12-14 16:24:04 -0300413 case V4L2_CID_BRIGHTNESS: return "Brightness";
414 case V4L2_CID_CONTRAST: return "Contrast";
415 case V4L2_CID_SATURATION: return "Saturation";
416 case V4L2_CID_HUE: return "Hue";
Hans Verkuil81dde912009-02-20 06:30:12 -0300417 case V4L2_CID_AUDIO_VOLUME: return "Volume";
418 case V4L2_CID_AUDIO_BALANCE: return "Balance";
419 case V4L2_CID_AUDIO_BASS: return "Bass";
420 case V4L2_CID_AUDIO_TREBLE: return "Treble";
421 case V4L2_CID_AUDIO_MUTE: return "Mute";
422 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Laurent Pinchart74980152008-12-14 16:24:04 -0300423 case V4L2_CID_BLACK_LEVEL: return "Black Level";
424 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
425 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
426 case V4L2_CID_RED_BALANCE: return "Red Balance";
427 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
428 case V4L2_CID_GAMMA: return "Gamma";
429 case V4L2_CID_EXPOSURE: return "Exposure";
430 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
431 case V4L2_CID_GAIN: return "Gain";
432 case V4L2_CID_HFLIP: return "Horizontal Flip";
433 case V4L2_CID_VFLIP: return "Vertical Flip";
434 case V4L2_CID_HCENTER: return "Horizontal Center";
435 case V4L2_CID_VCENTER: return "Vertical Center";
436 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
437 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
438 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
439 case V4L2_CID_SHARPNESS: return "Sharpness";
440 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
441 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
Devin Heitmuellerf1a4f9e2010-03-11 22:00:13 -0300442 case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
Laurent Pinchart74980152008-12-14 16:24:04 -0300443 case V4L2_CID_COLOR_KILLER: return "Color Killer";
Hans Verkuil11c469e2009-02-20 05:50:52 -0300444 case V4L2_CID_COLORFX: return "Color Effects";
Frank Schaefera3415c12010-03-14 14:28:54 -0300445 case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
446 case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
Vaibhav Hiremath85213632009-11-10 13:32:53 -0300447 case V4L2_CID_ROTATE: return "Rotate";
Frank Schaefera3415c12010-03-14 14:28:54 -0300448 case V4L2_CID_BG_COLOR: return "Background Color";
Hans Verkuil69028d72008-08-08 07:55:00 -0300449
450 /* MPEG controls */
451 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
452 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
453 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
454 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
455 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
456 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
Hans Verkuilf723af12008-08-09 10:35:29 -0300457 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
Hans Verkuil69028d72008-08-08 07:55:00 -0300458 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
459 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
460 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
461 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
462 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
463 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
464 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
465 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
466 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
467 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
468 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
469 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
470 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
471 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
472 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
473 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
474 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
475 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
476 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
477 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
478 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
479 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
480 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
481 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
482 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
483 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
484
Laurent Pinchart74980152008-12-14 16:24:04 -0300485 /* CAMERA controls */
486 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
487 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
488 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
489 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
490 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
491 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
492 case V4L2_CID_PAN_RESET: return "Pan, Reset";
493 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
494 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
495 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
496 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
497 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
498 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
Laurent Pinchart48213fe2010-01-20 12:12:57 -0300499 case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
500 case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
Laurent Pinchart74980152008-12-14 16:24:04 -0300501 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
502 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
503 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
504 case V4L2_CID_PRIVACY: return "Privacy";
505
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300506 /* FM Radio Modulator control */
507 case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
508 case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
509 case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
510 case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
511 case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
512 case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
513 case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
514 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
515 case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
516 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Feature Enabled";
517 case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
518 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
519 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
520 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
521 case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
522 case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
523 case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
524 case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-emphasis settings";
525 case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
526 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
527
Hans Verkuil69028d72008-08-08 07:55:00 -0300528 default:
529 return NULL;
530 }
531}
532EXPORT_SYMBOL(v4l2_ctrl_get_name);
533
Hans Verkuil9cb23182006-06-18 14:11:08 -0300534/* Fill in a struct v4l2_queryctrl */
535int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
536{
Hans Verkuil69028d72008-08-08 07:55:00 -0300537 const char *name = v4l2_ctrl_get_name(qctrl->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300538
539 qctrl->flags = 0;
Hans Verkuil69028d72008-08-08 07:55:00 -0300540 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300541 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300542
Hans Verkuil9cb23182006-06-18 14:11:08 -0300543 switch (qctrl->id) {
544 case V4L2_CID_AUDIO_MUTE:
545 case V4L2_CID_AUDIO_LOUDNESS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300546 case V4L2_CID_AUTO_WHITE_BALANCE:
547 case V4L2_CID_AUTOGAIN:
548 case V4L2_CID_HFLIP:
549 case V4L2_CID_VFLIP:
550 case V4L2_CID_HUE_AUTO:
Hans Verkuil81dde912009-02-20 06:30:12 -0300551 case V4L2_CID_CHROMA_AGC:
552 case V4L2_CID_COLOR_KILLER:
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300553 case V4L2_CID_MPEG_AUDIO_MUTE:
Hans Verkuil01f1e442007-08-21 18:32:42 -0300554 case V4L2_CID_MPEG_VIDEO_MUTE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300555 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
556 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
Laurent Pinchart74980152008-12-14 16:24:04 -0300557 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
Hans Verkuil81dde912009-02-20 06:30:12 -0300558 case V4L2_CID_FOCUS_AUTO:
Laurent Pinchart74980152008-12-14 16:24:04 -0300559 case V4L2_CID_PRIVACY:
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300560 case V4L2_CID_AUDIO_LIMITER_ENABLED:
561 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
562 case V4L2_CID_PILOT_TONE_ENABLED:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300563 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
564 min = 0;
565 max = step = 1;
566 break;
Hans Verkuil81dde912009-02-20 06:30:12 -0300567 case V4L2_CID_PAN_RESET:
568 case V4L2_CID_TILT_RESET:
569 qctrl->type = V4L2_CTRL_TYPE_BUTTON;
570 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
571 min = max = step = def = 0;
572 break;
Laurent Pinchart74980152008-12-14 16:24:04 -0300573 case V4L2_CID_POWER_LINE_FREQUENCY:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300574 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
575 case V4L2_CID_MPEG_AUDIO_ENCODING:
576 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
577 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
578 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
Hans Verkuile6b5da82008-08-08 07:38:07 -0300579 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300580 case V4L2_CID_MPEG_AUDIO_MODE:
581 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
582 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
583 case V4L2_CID_MPEG_AUDIO_CRC:
584 case V4L2_CID_MPEG_VIDEO_ENCODING:
585 case V4L2_CID_MPEG_VIDEO_ASPECT:
586 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
587 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil8cbde942006-06-24 14:36:02 -0300588 case V4L2_CID_MPEG_STREAM_VBI_FMT:
Laurent Pinchart74980152008-12-14 16:24:04 -0300589 case V4L2_CID_EXPOSURE_AUTO:
Hans Verkuil11c469e2009-02-20 05:50:52 -0300590 case V4L2_CID_COLORFX:
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300591 case V4L2_CID_TUNE_PREEMPHASIS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300592 qctrl->type = V4L2_CTRL_TYPE_MENU;
593 step = 1;
594 break;
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300595 case V4L2_CID_RDS_TX_PS_NAME:
596 case V4L2_CID_RDS_TX_RADIO_TEXT:
597 qctrl->type = V4L2_CTRL_TYPE_STRING;
598 break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300599 case V4L2_CID_USER_CLASS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300600 case V4L2_CID_CAMERA_CLASS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300601 case V4L2_CID_MPEG_CLASS:
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300602 case V4L2_CID_FM_TX_CLASS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300603 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
604 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
605 min = max = step = def = 0;
606 break;
Vaibhav Hiremath85213632009-11-10 13:32:53 -0300607 case V4L2_CID_BG_COLOR:
608 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
609 step = 1;
610 min = 0;
611 /* Max is calculated as RGB888 that is 2^24 */
612 max = 0xFFFFFF;
613 break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300614 default:
615 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
616 break;
617 }
618 switch (qctrl->id) {
619 case V4L2_CID_MPEG_AUDIO_ENCODING:
620 case V4L2_CID_MPEG_AUDIO_MODE:
621 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
622 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
623 case V4L2_CID_MPEG_STREAM_TYPE:
624 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
625 break;
626 case V4L2_CID_AUDIO_VOLUME:
627 case V4L2_CID_AUDIO_BALANCE:
628 case V4L2_CID_AUDIO_BASS:
629 case V4L2_CID_AUDIO_TREBLE:
630 case V4L2_CID_BRIGHTNESS:
631 case V4L2_CID_CONTRAST:
632 case V4L2_CID_SATURATION:
633 case V4L2_CID_HUE:
Hans Verkuil81dde912009-02-20 06:30:12 -0300634 case V4L2_CID_RED_BALANCE:
635 case V4L2_CID_BLUE_BALANCE:
636 case V4L2_CID_GAMMA:
Janne Grunau8c84cfd2009-03-18 17:00:39 -0300637 case V4L2_CID_SHARPNESS:
Devin Heitmuellerf1a4f9e2010-03-11 22:00:13 -0300638 case V4L2_CID_CHROMA_GAIN:
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300639 case V4L2_CID_RDS_TX_DEVIATION:
640 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
641 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
642 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
643 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
644 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
645 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
646 case V4L2_CID_PILOT_TONE_DEVIATION:
647 case V4L2_CID_PILOT_TONE_FREQUENCY:
648 case V4L2_CID_TUNE_POWER_LEVEL:
649 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300650 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
651 break;
Hans Verkuil81dde912009-02-20 06:30:12 -0300652 case V4L2_CID_PAN_RELATIVE:
653 case V4L2_CID_TILT_RELATIVE:
654 case V4L2_CID_FOCUS_RELATIVE:
Laurent Pinchart48213fe2010-01-20 12:12:57 -0300655 case V4L2_CID_IRIS_RELATIVE:
Hans Verkuil81dde912009-02-20 06:30:12 -0300656 case V4L2_CID_ZOOM_RELATIVE:
657 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
658 break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300659 }
660 qctrl->minimum = min;
661 qctrl->maximum = max;
662 qctrl->step = step;
663 qctrl->default_value = def;
664 qctrl->reserved[0] = qctrl->reserved[1] = 0;
Hans Verkuilb634a932009-01-17 11:26:59 -0300665 strlcpy(qctrl->name, name, sizeof(qctrl->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300666 return 0;
667}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300668EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300669
Hans Verkuil9cb23182006-06-18 14:11:08 -0300670/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300671 the menu. The qctrl pointer may be NULL, in which case it is ignored.
672 If menu_items is NULL, then the menu items are retrieved using
673 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300674int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
675 const char **menu_items)
676{
677 int i;
678
Hans Verkuil1e551262008-08-08 08:34:19 -0300679 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300680 if (menu_items == NULL)
681 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300682 if (menu_items == NULL ||
683 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
684 return -EINVAL;
685 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
686 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
687 return -EINVAL;
Hans Verkuilb634a932009-01-17 11:26:59 -0300688 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300689 return 0;
690}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300691EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300692
Hans Verkuil1e551262008-08-08 08:34:19 -0300693/* Fill in a struct v4l2_querymenu based on the specified array of valid
694 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
695 Use this if there are 'holes' in the list of valid menu items. */
696int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
697{
698 const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
699
700 qmenu->reserved = 0;
701 if (menu_items == NULL || ids == NULL)
702 return -EINVAL;
703 while (*ids != V4L2_CTRL_MENU_IDS_END) {
704 if (*ids++ == qmenu->index) {
Hans Verkuilb634a932009-01-17 11:26:59 -0300705 strlcpy(qmenu->name, menu_items[qmenu->index],
706 sizeof(qmenu->name));
Hans Verkuil1e551262008-08-08 08:34:19 -0300707 return 0;
708 }
709 }
710 return -EINVAL;
711}
712EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
713
Hans Verkuil9cb23182006-06-18 14:11:08 -0300714/* ctrl_classes points to an array of u32 pointers, the last element is
715 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
716 Each array must be sorted low to high and belong to the same control
Hans Verkuil2ba58892009-02-13 10:57:48 -0300717 class. The array of u32 pointers must also be sorted, from low class IDs
Hans Verkuil9cb23182006-06-18 14:11:08 -0300718 to high class IDs.
719
720 This function returns the first ID that follows after the given ID.
721 When no more controls are available 0 is returned. */
722u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
723{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300724 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300725 const u32 *pctrl;
726
Hans Verkuil9cb23182006-06-18 14:11:08 -0300727 if (ctrl_classes == NULL)
728 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300729
730 /* if no query is desired, then check if the ID is part of ctrl_classes */
731 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
732 /* find class */
733 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
734 ctrl_classes++;
735 if (*ctrl_classes == NULL)
736 return 0;
737 pctrl = *ctrl_classes;
738 /* find control ID */
739 while (*pctrl && *pctrl != id) pctrl++;
740 return *pctrl ? id : 0;
741 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300742 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300743 id++; /* select next control */
744 /* find first class that matches (or is greater than) the class of
745 the ID */
746 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
747 ctrl_classes++;
748 /* no more classes */
749 if (*ctrl_classes == NULL)
750 return 0;
751 pctrl = *ctrl_classes;
752 /* find first ctrl within the class that is >= ID */
753 while (*pctrl && *pctrl < id) pctrl++;
754 if (*pctrl)
755 return *pctrl;
756 /* we are at the end of the controls of the current class. */
757 /* continue with next class if available */
758 ctrl_classes++;
759 if (*ctrl_classes == NULL)
760 return 0;
761 return **ctrl_classes;
762}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300763EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300764
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300765int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300766{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300767 switch (match->type) {
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300768 case V4L2_CHIP_MATCH_HOST:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300769 return match->addr == 0;
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300770 default:
771 return 0;
772 }
773}
774EXPORT_SYMBOL(v4l2_chip_match_host);
775
776#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300777int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300778{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300779 int len;
780
781 if (c == NULL || match == NULL)
782 return 0;
783
784 switch (match->type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300785 case V4L2_CHIP_MATCH_I2C_DRIVER:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300786 if (c->driver == NULL || c->driver->driver.name == NULL)
787 return 0;
788 len = strlen(c->driver->driver.name);
789 /* legacy drivers have a ' suffix, don't try to match that */
790 if (len && c->driver->driver.name[len - 1] == '\'')
791 len--;
792 return len && !strncmp(c->driver->driver.name, match->name, len);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300793 case V4L2_CHIP_MATCH_I2C_ADDR:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300794 return c->addr == match->addr;
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300795 default:
796 return 0;
797 }
798}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300799EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300800
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300801int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
Hans Verkuil3434eb72007-04-27 12:31:08 -0300802 u32 ident, u32 revision)
803{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300804 if (!v4l2_chip_match_i2c_client(c, &chip->match))
Hans Verkuil3434eb72007-04-27 12:31:08 -0300805 return 0;
806 if (chip->ident == V4L2_IDENT_NONE) {
807 chip->ident = ident;
808 chip->revision = revision;
809 }
810 else {
811 chip->ident = V4L2_IDENT_AMBIGUOUS;
812 chip->revision = 0;
813 }
814 return 0;
815}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300816EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300817
Hans Verkuil9cb23182006-06-18 14:11:08 -0300818/* ----------------------------------------------------------------- */
819
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300820/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300821
Hans Verkuildd991202008-11-23 12:19:45 -0300822
823void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
824 const struct v4l2_subdev_ops *ops)
825{
826 v4l2_subdev_init(sd, ops);
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300827 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
Hans Verkuildd991202008-11-23 12:19:45 -0300828 /* the owner is the same as the i2c_client's driver owner */
829 sd->owner = client->driver->driver.owner;
830 /* i2c_client and v4l2_subdev point to one another */
831 v4l2_set_subdevdata(sd, client);
832 i2c_set_clientdata(client, sd);
833 /* initialize name */
834 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
835 client->driver->driver.name, i2c_adapter_id(client->adapter),
836 client->addr);
837}
838EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
839
840
841
Hans Verkuile6574f22009-04-01 03:57:53 -0300842/* Load an i2c sub-device. */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300843struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
844 struct i2c_adapter *adapter, const char *module_name,
845 struct i2c_board_info *info, const unsigned short *probe_addrs)
846{
847 struct v4l2_subdev *sd = NULL;
848 struct i2c_client *client;
849
850 BUG_ON(!v4l2_dev);
851
852 if (module_name)
853 request_module(module_name);
854
855 /* Create the i2c client */
856 if (info->addr == 0 && probe_addrs)
857 client = i2c_new_probed_device(adapter, info, probe_addrs);
858 else
859 client = i2c_new_device(adapter, info);
860
861 /* Note: by loading the module first we are certain that c->driver
862 will be set if the driver was found. If the module was not loaded
863 first, then the i2c core tries to delay-load the module for us,
864 and then c->driver is still NULL until the module is finally
865 loaded. This delay-load mechanism doesn't work if other drivers
866 want to use the i2c device, so explicitly loading the module
867 is the best alternative. */
868 if (client == NULL || client->driver == NULL)
869 goto error;
870
871 /* Lock the module so we can safely get the v4l2_subdev pointer */
872 if (!try_module_get(client->driver->driver.owner))
873 goto error;
874 sd = i2c_get_clientdata(client);
875
876 /* Register with the v4l2_device which increases the module's
877 use count as well. */
878 if (v4l2_device_register_subdev(v4l2_dev, sd))
879 sd = NULL;
880 /* Decrease the module use count to match the first try_module_get. */
881 module_put(client->driver->driver.owner);
882
883 if (sd) {
884 /* We return errors from v4l2_subdev_call only if we have the
885 callback as the .s_config is not mandatory */
886 int err = v4l2_subdev_call(sd, core, s_config,
887 info->irq, info->platform_data);
888
889 if (err && err != -ENOIOCTLCMD) {
890 v4l2_device_unregister_subdev(sd);
891 sd = NULL;
892 }
893 }
894
895error:
896 /* If we have a client but no subdev, then something went wrong and
897 we must unregister the client. */
898 if (client && sd == NULL)
899 i2c_unregister_device(client);
900 return sd;
901}
902EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
903
904struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev,
905 struct i2c_adapter *adapter,
906 const char *module_name, const char *client_type,
907 int irq, void *platform_data,
908 u8 addr, const unsigned short *probe_addrs)
909{
910 struct i2c_board_info info;
911
912 /* Setup the i2c board info with the device type and
913 the device address. */
914 memset(&info, 0, sizeof(info));
915 strlcpy(info.type, client_type, sizeof(info.type));
916 info.addr = addr;
917 info.irq = irq;
918 info.platform_data = platform_data;
919
920 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, module_name,
921 &info, probe_addrs);
922}
923EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg);
924
Hans Verkuilab3731902009-02-21 18:08:41 -0300925/* Return i2c client address of v4l2_subdev. */
926unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
927{
928 struct i2c_client *client = v4l2_get_subdevdata(sd);
929
930 return client ? client->addr : I2C_CLIENT_END;
931}
932EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
933
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300934/* Return a list of I2C tuner addresses to probe. Use only if the tuner
935 addresses are unknown. */
936const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
937{
938 static const unsigned short radio_addrs[] = {
939#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
940 0x10,
941#endif
942 0x60,
943 I2C_CLIENT_END
944 };
945 static const unsigned short demod_addrs[] = {
946 0x42, 0x43, 0x4a, 0x4b,
947 I2C_CLIENT_END
948 };
949 static const unsigned short tv_addrs[] = {
950 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Hans Verkuil416a7aa2009-05-02 09:42:57 -0300951 0x60, 0x61, 0x62, 0x63, 0x64,
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300952 I2C_CLIENT_END
953 };
954
955 switch (type) {
956 case ADDRS_RADIO:
957 return radio_addrs;
958 case ADDRS_DEMOD:
959 return demod_addrs;
960 case ADDRS_TV:
961 return tv_addrs;
962 case ADDRS_TV_WITH_DEMOD:
963 return tv_addrs + 4;
964 }
965 return NULL;
966}
967EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
968
Trent Piephod8b29962009-06-12 16:31:29 -0300969#endif /* defined(CONFIG_I2C) */
970
Dmitri Belimov85e09212010-03-23 11:23:29 -0300971#if defined(CONFIG_SPI)
972
973/* Load a spi sub-device. */
974
975void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
976 const struct v4l2_subdev_ops *ops)
977{
978 v4l2_subdev_init(sd, ops);
979 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
980 /* the owner is the same as the spi_device's driver owner */
981 sd->owner = spi->dev.driver->owner;
982 /* spi_device and v4l2_subdev point to one another */
983 v4l2_set_subdevdata(sd, spi);
984 spi_set_drvdata(spi, sd);
985 /* initialize name */
986 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
987}
988EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
989
990struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
991 struct spi_master *master, struct spi_board_info *info)
992{
993 struct v4l2_subdev *sd = NULL;
994 struct spi_device *spi = NULL;
995
996 BUG_ON(!v4l2_dev);
997
998 if (info->modalias)
999 request_module(info->modalias);
1000
1001 spi = spi_new_device(master, info);
1002
1003 if (spi == NULL || spi->dev.driver == NULL)
1004 goto error;
1005
1006 if (!try_module_get(spi->dev.driver->owner))
1007 goto error;
1008
1009 sd = spi_get_drvdata(spi);
1010
1011 /* Register with the v4l2_device which increases the module's
1012 use count as well. */
1013 if (v4l2_device_register_subdev(v4l2_dev, sd))
1014 sd = NULL;
1015
1016 /* Decrease the module use count to match the first try_module_get. */
1017 module_put(spi->dev.driver->owner);
1018
1019error:
1020 /* If we have a client but no subdev, then something went wrong and
1021 we must unregister the client. */
1022 if (spi && sd == NULL)
1023 spi_unregister_device(spi);
1024
1025 return sd;
1026}
1027EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
1028
1029#endif /* defined(CONFIG_SPI) */
1030
Trent Piephob0d31592009-05-30 21:45:46 -03001031/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
1032 * and max don't have to be aligned, but there must be at least one valid
1033 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
1034 * of 16 between 17 and 31. */
1035static unsigned int clamp_align(unsigned int x, unsigned int min,
1036 unsigned int max, unsigned int align)
1037{
1038 /* Bits that must be zero to be aligned */
1039 unsigned int mask = ~((1 << align) - 1);
1040
1041 /* Round to nearest aligned value */
1042 if (align)
1043 x = (x + (1 << (align - 1))) & mask;
1044
1045 /* Clamp to aligned value of min and max */
1046 if (x < min)
1047 x = (min + ~mask) & mask;
1048 else if (x > max)
1049 x = max & mask;
1050
1051 return x;
1052}
1053
1054/* Bound an image to have a width between wmin and wmax, and height between
1055 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
1056 * 2^walign, the height will be a multiple of 2^halign, and the overall size
1057 * (width*height) will be a multiple of 2^salign. The image may be shrunk
1058 * or enlarged to fit the alignment constraints.
1059 *
1060 * The width or height maximum must not be smaller than the corresponding
1061 * minimum. The alignments must not be so high there are no possible image
1062 * sizes within the allowed bounds. wmin and hmin must be at least 1
1063 * (don't use 0). If you don't care about a certain alignment, specify 0,
1064 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
1065 * you only want to adjust downward, specify a maximum that's the same as
1066 * the initial value.
1067 */
1068void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
1069 unsigned int walign,
1070 u32 *h, unsigned int hmin, unsigned int hmax,
1071 unsigned int halign, unsigned int salign)
1072{
1073 *w = clamp_align(*w, wmin, wmax, walign);
1074 *h = clamp_align(*h, hmin, hmax, halign);
1075
1076 /* Usually we don't need to align the size and are done now. */
1077 if (!salign)
1078 return;
1079
1080 /* How much alignment do we have? */
1081 walign = __ffs(*w);
1082 halign = __ffs(*h);
1083 /* Enough to satisfy the image alignment? */
1084 if (walign + halign < salign) {
1085 /* Max walign where there is still a valid width */
1086 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
1087 /* Max halign where there is still a valid height */
1088 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
1089
1090 /* up the smaller alignment until we have enough */
1091 do {
1092 if (halign >= hmaxa ||
1093 (walign <= halign && walign < wmaxa)) {
1094 *w = clamp_align(*w, wmin, wmax, walign + 1);
1095 walign = __ffs(*w);
1096 } else {
1097 *h = clamp_align(*h, hmin, hmax, halign + 1);
1098 halign = __ffs(*h);
1099 }
1100 } while (halign + walign < salign);
1101 }
1102}
1103EXPORT_SYMBOL_GPL(v4l_bound_align_image);
Muralidharan Karicheri2e535ed2009-12-10 04:39:47 -03001104
1105/**
1106 * v4l_fill_dv_preset_info - fill description of a digital video preset
1107 * @preset - preset value
1108 * @info - pointer to struct v4l2_dv_enum_preset
1109 *
1110 * drivers can use this helper function to fill description of dv preset
1111 * in info.
1112 */
1113int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
1114{
1115 static const struct v4l2_dv_preset_info {
1116 u16 width;
1117 u16 height;
1118 const char *name;
1119 } dv_presets[] = {
1120 { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */
1121 { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */
1122 { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */
1123 { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */
1124 { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */
1125 { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */
1126 { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */
1127 { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */
1128 { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */
1129 { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */
1130 { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */
1131 { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */
1132 { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */
1133 { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */
1134 { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */
1135 { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */
1136 { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */
1137 { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */
1138 { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */
1139 };
1140
1141 if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
1142 return -EINVAL;
1143
1144 info->preset = preset;
1145 info->width = dv_presets[preset].width;
1146 info->height = dv_presets[preset].height;
1147 strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
1148 return 0;
1149}
1150EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);