blob: 536150c3406771eeb4d6e4721e2a5c48a0b85605 [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>
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 Verkuildd991202008-11-23 12:19:45 -030061#include <media/v4l2-device.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030062#include <media/v4l2-chip-ident.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Hans Verkuil33b687c2008-07-25 05:32:50 -030064#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
67MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
68MODULE_LICENSE("GPL");
69
70/*
71 *
72 * V 4 L 2 D R I V E R H E L P E R A P I
73 *
74 */
75
76/*
77 * Video Standard Operations (contributed by Michael Schimek)
78 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* ----------------------------------------------------------------- */
82/* priority handling */
83
84#define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND || \
85 val == V4L2_PRIORITY_INTERACTIVE || \
86 val == V4L2_PRIORITY_RECORD)
87
88int v4l2_prio_init(struct v4l2_prio_state *global)
89{
90 memset(global,0,sizeof(*global));
91 return 0;
92}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -030093EXPORT_SYMBOL(v4l2_prio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
96 enum v4l2_priority new)
97{
98 if (!V4L2_PRIO_VALID(new))
99 return -EINVAL;
100 if (*local == new)
101 return 0;
102
103 atomic_inc(&global->prios[new]);
104 if (V4L2_PRIO_VALID(*local))
105 atomic_dec(&global->prios[*local]);
106 *local = new;
107 return 0;
108}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300109EXPORT_SYMBOL(v4l2_prio_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111int v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
112{
113 return v4l2_prio_change(global,local,V4L2_PRIORITY_DEFAULT);
114}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300115EXPORT_SYMBOL(v4l2_prio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117int v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority *local)
118{
119 if (V4L2_PRIO_VALID(*local))
120 atomic_dec(&global->prios[*local]);
121 return 0;
122}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300123EXPORT_SYMBOL(v4l2_prio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
126{
127 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
128 return V4L2_PRIORITY_RECORD;
129 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
130 return V4L2_PRIORITY_INTERACTIVE;
131 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
132 return V4L2_PRIORITY_BACKGROUND;
133 return V4L2_PRIORITY_UNSET;
134}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300135EXPORT_SYMBOL(v4l2_prio_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority *local)
138{
139 if (*local < v4l2_prio_max(global))
140 return -EBUSY;
141 return 0;
142}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300143EXPORT_SYMBOL(v4l2_prio_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* ----------------------------------------------------------------- */
146
Hans Verkuil9cb23182006-06-18 14:11:08 -0300147/* Helper functions for control handling */
148
149/* Check for correctness of the ctrl's value based on the data from
150 struct v4l2_queryctrl and the available menu items. Note that
151 menu_items may be NULL, in that case it is ignored. */
152int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
153 const char **menu_items)
154{
155 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
156 return -EINVAL;
157 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
158 return -EBUSY;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300159 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
160 return 0;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300161 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
162 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
163 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
164 return 0;
165 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
166 return -ERANGE;
167 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
168 if (menu_items[ctrl->value] == NULL ||
169 menu_items[ctrl->value][0] == '\0')
170 return -EINVAL;
171 }
172 return 0;
173}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300174EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300175
176/* Returns NULL or a character pointer array containing the menu for
177 the given control ID. The pointer array ends with a NULL pointer.
178 An empty string signifies a menu entry that is invalid. This allows
179 drivers to disable certain options if it is not supported. */
180const char **v4l2_ctrl_get_menu(u32 id)
181{
182 static const char *mpeg_audio_sampling_freq[] = {
183 "44.1 kHz",
184 "48 kHz",
185 "32 kHz",
186 NULL
187 };
188 static const char *mpeg_audio_encoding[] = {
Hans Verkuile6b5da82008-08-08 07:38:07 -0300189 "MPEG-1/2 Layer I",
190 "MPEG-1/2 Layer II",
191 "MPEG-1/2 Layer III",
192 "MPEG-2/4 AAC",
193 "AC-3",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300194 NULL
195 };
196 static const char *mpeg_audio_l1_bitrate[] = {
197 "32 kbps",
198 "64 kbps",
199 "96 kbps",
200 "128 kbps",
201 "160 kbps",
202 "192 kbps",
203 "224 kbps",
204 "256 kbps",
205 "288 kbps",
206 "320 kbps",
207 "352 kbps",
208 "384 kbps",
209 "416 kbps",
210 "448 kbps",
211 NULL
212 };
213 static const char *mpeg_audio_l2_bitrate[] = {
214 "32 kbps",
215 "48 kbps",
216 "56 kbps",
217 "64 kbps",
218 "80 kbps",
219 "96 kbps",
220 "112 kbps",
221 "128 kbps",
222 "160 kbps",
223 "192 kbps",
224 "224 kbps",
225 "256 kbps",
226 "320 kbps",
227 "384 kbps",
228 NULL
229 };
230 static const char *mpeg_audio_l3_bitrate[] = {
231 "32 kbps",
232 "40 kbps",
233 "48 kbps",
234 "56 kbps",
235 "64 kbps",
236 "80 kbps",
237 "96 kbps",
238 "112 kbps",
239 "128 kbps",
240 "160 kbps",
241 "192 kbps",
242 "224 kbps",
243 "256 kbps",
244 "320 kbps",
245 NULL
246 };
Hans Verkuile6b5da82008-08-08 07:38:07 -0300247 static const char *mpeg_audio_ac3_bitrate[] = {
248 "32 kbps",
249 "40 kbps",
250 "48 kbps",
251 "56 kbps",
252 "64 kbps",
253 "80 kbps",
254 "96 kbps",
255 "112 kbps",
256 "128 kbps",
257 "160 kbps",
258 "192 kbps",
259 "224 kbps",
260 "256 kbps",
261 "320 kbps",
262 "384 kbps",
263 "448 kbps",
264 "512 kbps",
265 "576 kbps",
266 "640 kbps",
267 NULL
268 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300269 static const char *mpeg_audio_mode[] = {
270 "Stereo",
271 "Joint Stereo",
272 "Dual",
273 "Mono",
274 NULL
275 };
276 static const char *mpeg_audio_mode_extension[] = {
277 "Bound 4",
278 "Bound 8",
279 "Bound 12",
280 "Bound 16",
281 NULL
282 };
283 static const char *mpeg_audio_emphasis[] = {
284 "No Emphasis",
285 "50/15 us",
286 "CCITT J17",
287 NULL
288 };
289 static const char *mpeg_audio_crc[] = {
290 "No CRC",
291 "16-bit CRC",
292 NULL
293 };
294 static const char *mpeg_video_encoding[] = {
295 "MPEG-1",
296 "MPEG-2",
Janne Grunau188919a2008-08-08 07:21:00 -0300297 "MPEG-4 AVC",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300298 NULL
299 };
300 static const char *mpeg_video_aspect[] = {
301 "1x1",
302 "4x3",
303 "16x9",
304 "2.21x1",
305 NULL
306 };
307 static const char *mpeg_video_bitrate_mode[] = {
308 "Variable Bitrate",
309 "Constant Bitrate",
310 NULL
311 };
312 static const char *mpeg_stream_type[] = {
313 "MPEG-2 Program Stream",
314 "MPEG-2 Transport Stream",
315 "MPEG-1 System Stream",
316 "MPEG-2 DVD-compatible Stream",
317 "MPEG-1 VCD-compatible Stream",
318 "MPEG-2 SVCD-compatible Stream",
319 NULL
320 };
Hans Verkuil8cbde942006-06-24 14:36:02 -0300321 static const char *mpeg_stream_vbi_fmt[] = {
322 "No VBI",
Hans Verkuil99523512006-06-25 11:18:54 -0300323 "Private packet, IVTV format",
Hans Verkuil8cbde942006-06-24 14:36:02 -0300324 NULL
325 };
Laurent Pinchart74980152008-12-14 16:24:04 -0300326 static const char *camera_power_line_frequency[] = {
327 "Disabled",
328 "50 Hz",
329 "60 Hz",
330 NULL
331 };
332 static const char *camera_exposure_auto[] = {
333 "Auto Mode",
334 "Manual Mode",
335 "Shutter Priority Mode",
336 "Aperture Priority Mode",
337 NULL
338 };
Hans Verkuil11c469e2009-02-20 05:50:52 -0300339 static const char *colorfx[] = {
340 "None",
341 "Black & White",
342 "Sepia",
343 NULL
344 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300345
346 switch (id) {
347 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
348 return mpeg_audio_sampling_freq;
349 case V4L2_CID_MPEG_AUDIO_ENCODING:
350 return mpeg_audio_encoding;
351 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
352 return mpeg_audio_l1_bitrate;
353 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
354 return mpeg_audio_l2_bitrate;
355 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
356 return mpeg_audio_l3_bitrate;
Hans Verkuile6b5da82008-08-08 07:38:07 -0300357 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
358 return mpeg_audio_ac3_bitrate;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300359 case V4L2_CID_MPEG_AUDIO_MODE:
360 return mpeg_audio_mode;
361 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
362 return mpeg_audio_mode_extension;
363 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
364 return mpeg_audio_emphasis;
365 case V4L2_CID_MPEG_AUDIO_CRC:
366 return mpeg_audio_crc;
367 case V4L2_CID_MPEG_VIDEO_ENCODING:
368 return mpeg_video_encoding;
369 case V4L2_CID_MPEG_VIDEO_ASPECT:
370 return mpeg_video_aspect;
371 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
372 return mpeg_video_bitrate_mode;
373 case V4L2_CID_MPEG_STREAM_TYPE:
374 return mpeg_stream_type;
Hans Verkuil8cbde942006-06-24 14:36:02 -0300375 case V4L2_CID_MPEG_STREAM_VBI_FMT:
376 return mpeg_stream_vbi_fmt;
Laurent Pinchart74980152008-12-14 16:24:04 -0300377 case V4L2_CID_POWER_LINE_FREQUENCY:
378 return camera_power_line_frequency;
379 case V4L2_CID_EXPOSURE_AUTO:
380 return camera_exposure_auto;
Hans Verkuil11c469e2009-02-20 05:50:52 -0300381 case V4L2_CID_COLORFX:
382 return colorfx;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300383 default:
384 return NULL;
385 }
386}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300387EXPORT_SYMBOL(v4l2_ctrl_get_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300388
Hans Verkuil69028d72008-08-08 07:55:00 -0300389/* Return the control name. */
390const char *v4l2_ctrl_get_name(u32 id)
391{
392 switch (id) {
393 /* USER controls */
Laurent Pinchart74980152008-12-14 16:24:04 -0300394 case V4L2_CID_USER_CLASS: return "User Controls";
Laurent Pinchart74980152008-12-14 16:24:04 -0300395 case V4L2_CID_BRIGHTNESS: return "Brightness";
396 case V4L2_CID_CONTRAST: return "Contrast";
397 case V4L2_CID_SATURATION: return "Saturation";
398 case V4L2_CID_HUE: return "Hue";
Hans Verkuil81dde912009-02-20 06:30:12 -0300399 case V4L2_CID_AUDIO_VOLUME: return "Volume";
400 case V4L2_CID_AUDIO_BALANCE: return "Balance";
401 case V4L2_CID_AUDIO_BASS: return "Bass";
402 case V4L2_CID_AUDIO_TREBLE: return "Treble";
403 case V4L2_CID_AUDIO_MUTE: return "Mute";
404 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
Laurent Pinchart74980152008-12-14 16:24:04 -0300405 case V4L2_CID_BLACK_LEVEL: return "Black Level";
406 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
407 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
408 case V4L2_CID_RED_BALANCE: return "Red Balance";
409 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
410 case V4L2_CID_GAMMA: return "Gamma";
411 case V4L2_CID_EXPOSURE: return "Exposure";
412 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
413 case V4L2_CID_GAIN: return "Gain";
414 case V4L2_CID_HFLIP: return "Horizontal Flip";
415 case V4L2_CID_VFLIP: return "Vertical Flip";
416 case V4L2_CID_HCENTER: return "Horizontal Center";
417 case V4L2_CID_VCENTER: return "Vertical Center";
418 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
419 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
420 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
421 case V4L2_CID_SHARPNESS: return "Sharpness";
422 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
423 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
424 case V4L2_CID_COLOR_KILLER: return "Color Killer";
Hans Verkuil11c469e2009-02-20 05:50:52 -0300425 case V4L2_CID_COLORFX: return "Color Effects";
Hans Verkuil69028d72008-08-08 07:55:00 -0300426
427 /* MPEG controls */
428 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
429 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
430 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
431 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
432 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
433 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
Hans Verkuilf723af12008-08-09 10:35:29 -0300434 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
Hans Verkuil69028d72008-08-08 07:55:00 -0300435 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
436 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
437 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
438 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
439 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
440 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
441 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
442 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
443 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
444 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
445 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
446 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
447 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
448 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
449 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
450 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
451 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
452 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
453 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
454 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
455 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
456 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
457 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
458 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
459 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
460 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
461
Laurent Pinchart74980152008-12-14 16:24:04 -0300462 /* CAMERA controls */
463 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
464 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
465 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
466 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
467 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
468 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
469 case V4L2_CID_PAN_RESET: return "Pan, Reset";
470 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
471 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
472 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
473 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
474 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
475 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
476 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
477 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
478 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
479 case V4L2_CID_PRIVACY: return "Privacy";
480
Hans Verkuil69028d72008-08-08 07:55:00 -0300481 default:
482 return NULL;
483 }
484}
485EXPORT_SYMBOL(v4l2_ctrl_get_name);
486
Hans Verkuil9cb23182006-06-18 14:11:08 -0300487/* Fill in a struct v4l2_queryctrl */
488int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
489{
Hans Verkuil69028d72008-08-08 07:55:00 -0300490 const char *name = v4l2_ctrl_get_name(qctrl->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300491
492 qctrl->flags = 0;
Hans Verkuil69028d72008-08-08 07:55:00 -0300493 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300494 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300495
Hans Verkuil9cb23182006-06-18 14:11:08 -0300496 switch (qctrl->id) {
497 case V4L2_CID_AUDIO_MUTE:
498 case V4L2_CID_AUDIO_LOUDNESS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300499 case V4L2_CID_AUTO_WHITE_BALANCE:
500 case V4L2_CID_AUTOGAIN:
501 case V4L2_CID_HFLIP:
502 case V4L2_CID_VFLIP:
503 case V4L2_CID_HUE_AUTO:
Hans Verkuil81dde912009-02-20 06:30:12 -0300504 case V4L2_CID_CHROMA_AGC:
505 case V4L2_CID_COLOR_KILLER:
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300506 case V4L2_CID_MPEG_AUDIO_MUTE:
Hans Verkuil01f1e442007-08-21 18:32:42 -0300507 case V4L2_CID_MPEG_VIDEO_MUTE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300508 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
509 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
Laurent Pinchart74980152008-12-14 16:24:04 -0300510 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
Hans Verkuil81dde912009-02-20 06:30:12 -0300511 case V4L2_CID_FOCUS_AUTO:
Laurent Pinchart74980152008-12-14 16:24:04 -0300512 case V4L2_CID_PRIVACY:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300513 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
514 min = 0;
515 max = step = 1;
516 break;
Hans Verkuil81dde912009-02-20 06:30:12 -0300517 case V4L2_CID_PAN_RESET:
518 case V4L2_CID_TILT_RESET:
519 qctrl->type = V4L2_CTRL_TYPE_BUTTON;
520 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
521 min = max = step = def = 0;
522 break;
Laurent Pinchart74980152008-12-14 16:24:04 -0300523 case V4L2_CID_POWER_LINE_FREQUENCY:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300524 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
525 case V4L2_CID_MPEG_AUDIO_ENCODING:
526 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
527 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
528 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
Hans Verkuile6b5da82008-08-08 07:38:07 -0300529 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300530 case V4L2_CID_MPEG_AUDIO_MODE:
531 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
532 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
533 case V4L2_CID_MPEG_AUDIO_CRC:
534 case V4L2_CID_MPEG_VIDEO_ENCODING:
535 case V4L2_CID_MPEG_VIDEO_ASPECT:
536 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
537 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil8cbde942006-06-24 14:36:02 -0300538 case V4L2_CID_MPEG_STREAM_VBI_FMT:
Laurent Pinchart74980152008-12-14 16:24:04 -0300539 case V4L2_CID_EXPOSURE_AUTO:
Hans Verkuil11c469e2009-02-20 05:50:52 -0300540 case V4L2_CID_COLORFX:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300541 qctrl->type = V4L2_CTRL_TYPE_MENU;
542 step = 1;
543 break;
544 case V4L2_CID_USER_CLASS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300545 case V4L2_CID_CAMERA_CLASS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300546 case V4L2_CID_MPEG_CLASS:
547 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
548 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
549 min = max = step = def = 0;
550 break;
551 default:
552 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
553 break;
554 }
555 switch (qctrl->id) {
556 case V4L2_CID_MPEG_AUDIO_ENCODING:
557 case V4L2_CID_MPEG_AUDIO_MODE:
558 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
559 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
560 case V4L2_CID_MPEG_STREAM_TYPE:
561 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
562 break;
563 case V4L2_CID_AUDIO_VOLUME:
564 case V4L2_CID_AUDIO_BALANCE:
565 case V4L2_CID_AUDIO_BASS:
566 case V4L2_CID_AUDIO_TREBLE:
567 case V4L2_CID_BRIGHTNESS:
568 case V4L2_CID_CONTRAST:
569 case V4L2_CID_SATURATION:
570 case V4L2_CID_HUE:
Hans Verkuil81dde912009-02-20 06:30:12 -0300571 case V4L2_CID_RED_BALANCE:
572 case V4L2_CID_BLUE_BALANCE:
573 case V4L2_CID_GAMMA:
Janne Grunau8c84cfd2009-03-18 17:00:39 -0300574 case V4L2_CID_SHARPNESS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300575 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
576 break;
Hans Verkuil81dde912009-02-20 06:30:12 -0300577 case V4L2_CID_PAN_RELATIVE:
578 case V4L2_CID_TILT_RELATIVE:
579 case V4L2_CID_FOCUS_RELATIVE:
580 case V4L2_CID_ZOOM_RELATIVE:
581 qctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
582 break;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300583 }
584 qctrl->minimum = min;
585 qctrl->maximum = max;
586 qctrl->step = step;
587 qctrl->default_value = def;
588 qctrl->reserved[0] = qctrl->reserved[1] = 0;
Hans Verkuilb634a932009-01-17 11:26:59 -0300589 strlcpy(qctrl->name, name, sizeof(qctrl->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300590 return 0;
591}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300592EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300593
Hans Verkuil9cb23182006-06-18 14:11:08 -0300594/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300595 the menu. The qctrl pointer may be NULL, in which case it is ignored.
596 If menu_items is NULL, then the menu items are retrieved using
597 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300598int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
599 const char **menu_items)
600{
601 int i;
602
Hans Verkuil1e551262008-08-08 08:34:19 -0300603 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300604 if (menu_items == NULL)
605 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300606 if (menu_items == NULL ||
607 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
608 return -EINVAL;
609 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
610 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
611 return -EINVAL;
Hans Verkuilb634a932009-01-17 11:26:59 -0300612 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300613 return 0;
614}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300615EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300616
Hans Verkuil1e551262008-08-08 08:34:19 -0300617/* Fill in a struct v4l2_querymenu based on the specified array of valid
618 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
619 Use this if there are 'holes' in the list of valid menu items. */
620int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
621{
622 const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
623
624 qmenu->reserved = 0;
625 if (menu_items == NULL || ids == NULL)
626 return -EINVAL;
627 while (*ids != V4L2_CTRL_MENU_IDS_END) {
628 if (*ids++ == qmenu->index) {
Hans Verkuilb634a932009-01-17 11:26:59 -0300629 strlcpy(qmenu->name, menu_items[qmenu->index],
630 sizeof(qmenu->name));
Hans Verkuil1e551262008-08-08 08:34:19 -0300631 return 0;
632 }
633 }
634 return -EINVAL;
635}
636EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
637
Hans Verkuil9cb23182006-06-18 14:11:08 -0300638/* ctrl_classes points to an array of u32 pointers, the last element is
639 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
640 Each array must be sorted low to high and belong to the same control
Hans Verkuil2ba58892009-02-13 10:57:48 -0300641 class. The array of u32 pointers must also be sorted, from low class IDs
Hans Verkuil9cb23182006-06-18 14:11:08 -0300642 to high class IDs.
643
644 This function returns the first ID that follows after the given ID.
645 When no more controls are available 0 is returned. */
646u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
647{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300648 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300649 const u32 *pctrl;
650
Hans Verkuil9cb23182006-06-18 14:11:08 -0300651 if (ctrl_classes == NULL)
652 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300653
654 /* if no query is desired, then check if the ID is part of ctrl_classes */
655 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
656 /* find class */
657 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
658 ctrl_classes++;
659 if (*ctrl_classes == NULL)
660 return 0;
661 pctrl = *ctrl_classes;
662 /* find control ID */
663 while (*pctrl && *pctrl != id) pctrl++;
664 return *pctrl ? id : 0;
665 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300666 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300667 id++; /* select next control */
668 /* find first class that matches (or is greater than) the class of
669 the ID */
670 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
671 ctrl_classes++;
672 /* no more classes */
673 if (*ctrl_classes == NULL)
674 return 0;
675 pctrl = *ctrl_classes;
676 /* find first ctrl within the class that is >= ID */
677 while (*pctrl && *pctrl < id) pctrl++;
678 if (*pctrl)
679 return *pctrl;
680 /* we are at the end of the controls of the current class. */
681 /* continue with next class if available */
682 ctrl_classes++;
683 if (*ctrl_classes == NULL)
684 return 0;
685 return **ctrl_classes;
686}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300687EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300688
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300689int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300690{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300691 switch (match->type) {
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300692 case V4L2_CHIP_MATCH_HOST:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300693 return match->addr == 0;
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300694 default:
695 return 0;
696 }
697}
698EXPORT_SYMBOL(v4l2_chip_match_host);
699
700#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300701int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300702{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300703 int len;
704
705 if (c == NULL || match == NULL)
706 return 0;
707
708 switch (match->type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300709 case V4L2_CHIP_MATCH_I2C_DRIVER:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300710 if (c->driver == NULL || c->driver->driver.name == NULL)
711 return 0;
712 len = strlen(c->driver->driver.name);
713 /* legacy drivers have a ' suffix, don't try to match that */
714 if (len && c->driver->driver.name[len - 1] == '\'')
715 len--;
716 return len && !strncmp(c->driver->driver.name, match->name, len);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300717 case V4L2_CHIP_MATCH_I2C_ADDR:
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300718 return c->addr == match->addr;
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300719 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 Verkuilaecde8b52008-12-30 07:14:19 -0300725int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
Hans Verkuil3434eb72007-04-27 12:31:08 -0300726 u32 ident, u32 revision)
727{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300728 if (!v4l2_chip_match_i2c_client(c, &chip->match))
Hans Verkuil3434eb72007-04-27 12:31:08 -0300729 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 Verkuil78a3b4d2009-04-01 03:41:09 -0300744/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300745
Hans Verkuildd991202008-11-23 12:19:45 -0300746
747void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
748 const struct v4l2_subdev_ops *ops)
749{
750 v4l2_subdev_init(sd, ops);
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300751 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
Hans Verkuildd991202008-11-23 12:19:45 -0300752 /* the owner is the same as the i2c_client's driver owner */
753 sd->owner = client->driver->driver.owner;
754 /* i2c_client and v4l2_subdev point to one another */
755 v4l2_set_subdevdata(sd, client);
756 i2c_set_clientdata(client, sd);
757 /* initialize name */
758 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
759 client->driver->driver.name, i2c_adapter_id(client->adapter),
760 client->addr);
761}
762EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
763
764
765
Hans Verkuile6574f22009-04-01 03:57:53 -0300766/* Load an i2c sub-device. */
767struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
768 struct i2c_adapter *adapter,
Hans Verkuildd991202008-11-23 12:19:45 -0300769 const char *module_name, const char *client_type, u8 addr)
770{
Hans Verkuildd991202008-11-23 12:19:45 -0300771 struct v4l2_subdev *sd = NULL;
772 struct i2c_client *client;
773 struct i2c_board_info info;
774
Hans Verkuile6574f22009-04-01 03:57:53 -0300775 BUG_ON(!v4l2_dev);
Hans Verkuild64260d2009-03-18 15:48:01 -0300776
Hans Verkuildd991202008-11-23 12:19:45 -0300777 if (module_name)
778 request_module(module_name);
Hans Verkuild64260d2009-03-18 15:48:01 -0300779
Hans Verkuildd991202008-11-23 12:19:45 -0300780 /* Setup the i2c board info with the device type and
781 the device address. */
782 memset(&info, 0, sizeof(info));
783 strlcpy(info.type, client_type, sizeof(info.type));
784 info.addr = addr;
785
786 /* Create the i2c client */
787 client = i2c_new_device(adapter, &info);
788 /* Note: it is possible in the future that
789 c->driver is NULL if the driver is still being loaded.
790 We need better support from the kernel so that we
791 can easily wait for the load to finish. */
792 if (client == NULL || client->driver == NULL)
Hans Verkuil7c026c32009-03-12 18:56:15 -0300793 goto error;
Hans Verkuildd991202008-11-23 12:19:45 -0300794
795 /* Lock the module so we can safely get the v4l2_subdev pointer */
796 if (!try_module_get(client->driver->driver.owner))
Hans Verkuil7c026c32009-03-12 18:56:15 -0300797 goto error;
Hans Verkuildd991202008-11-23 12:19:45 -0300798 sd = i2c_get_clientdata(client);
799
800 /* Register with the v4l2_device which increases the module's
801 use count as well. */
Hans Verkuile6574f22009-04-01 03:57:53 -0300802 if (v4l2_device_register_subdev(v4l2_dev, sd))
Hans Verkuildd991202008-11-23 12:19:45 -0300803 sd = NULL;
804 /* Decrease the module use count to match the first try_module_get. */
805 module_put(client->driver->driver.owner);
Hans Verkuildd991202008-11-23 12:19:45 -0300806
Hans Verkuilf0222c72009-06-09 17:12:33 -0300807 if (sd) {
808 /* We return errors from v4l2_subdev_call only if we have the
809 callback as the .s_config is not mandatory */
810 int err = v4l2_subdev_call(sd, core, s_config, 0, NULL);
811
812 if (err && err != -ENOIOCTLCMD) {
813 v4l2_device_unregister_subdev(sd);
814 sd = NULL;
815 }
816 }
817
Hans Verkuil7c026c32009-03-12 18:56:15 -0300818error:
819 /* If we have a client but no subdev, then something went wrong and
820 we must unregister the client. */
821 if (client && sd == NULL)
822 i2c_unregister_device(client);
823 return sd;
Hans Verkuildd991202008-11-23 12:19:45 -0300824}
825EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
826
Hans Verkuile6574f22009-04-01 03:57:53 -0300827/* Probe and load an i2c sub-device. */
828struct v4l2_subdev *v4l2_i2c_new_probed_subdev(struct v4l2_device *v4l2_dev,
829 struct i2c_adapter *adapter,
Hans Verkuildd991202008-11-23 12:19:45 -0300830 const char *module_name, const char *client_type,
831 const unsigned short *addrs)
832{
Hans Verkuildd991202008-11-23 12:19:45 -0300833 struct v4l2_subdev *sd = NULL;
834 struct i2c_client *client = NULL;
835 struct i2c_board_info info;
836
Hans Verkuile6574f22009-04-01 03:57:53 -0300837 BUG_ON(!v4l2_dev);
Hans Verkuild64260d2009-03-18 15:48:01 -0300838
Hans Verkuildd991202008-11-23 12:19:45 -0300839 if (module_name)
840 request_module(module_name);
Hans Verkuild64260d2009-03-18 15:48:01 -0300841
Hans Verkuildd991202008-11-23 12:19:45 -0300842 /* Setup the i2c board info with the device type and
843 the device address. */
844 memset(&info, 0, sizeof(info));
845 strlcpy(info.type, client_type, sizeof(info.type));
846
847 /* Probe and create the i2c client */
848 client = i2c_new_probed_device(adapter, &info, addrs);
849 /* Note: it is possible in the future that
850 c->driver is NULL if the driver is still being loaded.
851 We need better support from the kernel so that we
852 can easily wait for the load to finish. */
853 if (client == NULL || client->driver == NULL)
Hans Verkuil7c026c32009-03-12 18:56:15 -0300854 goto error;
Hans Verkuildd991202008-11-23 12:19:45 -0300855
856 /* Lock the module so we can safely get the v4l2_subdev pointer */
857 if (!try_module_get(client->driver->driver.owner))
Hans Verkuil7c026c32009-03-12 18:56:15 -0300858 goto error;
Hans Verkuildd991202008-11-23 12:19:45 -0300859 sd = i2c_get_clientdata(client);
860
861 /* Register with the v4l2_device which increases the module's
862 use count as well. */
Hans Verkuile6574f22009-04-01 03:57:53 -0300863 if (v4l2_device_register_subdev(v4l2_dev, sd))
Hans Verkuildd991202008-11-23 12:19:45 -0300864 sd = NULL;
865 /* Decrease the module use count to match the first try_module_get. */
866 module_put(client->driver->driver.owner);
Hans Verkuil7c026c32009-03-12 18:56:15 -0300867
Hans Verkuilf0222c72009-06-09 17:12:33 -0300868 if (sd) {
869 /* We return errors from v4l2_subdev_call only if we have the
870 callback as the .s_config is not mandatory */
871 int err = v4l2_subdev_call(sd, core, s_config, 0, NULL);
872
873 if (err && err != -ENOIOCTLCMD) {
874 v4l2_device_unregister_subdev(sd);
875 sd = NULL;
876 }
877 }
878
Hans Verkuil7c026c32009-03-12 18:56:15 -0300879error:
880 /* If we have a client but no subdev, then something went wrong and
881 we must unregister the client. */
882 if (client && sd == NULL)
883 i2c_unregister_device(client);
Hans Verkuildd991202008-11-23 12:19:45 -0300884 return sd;
885}
886EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev);
887
Hans Verkuil868f9852009-03-30 11:40:54 -0300888struct v4l2_subdev *v4l2_i2c_new_probed_subdev_addr(struct v4l2_device *v4l2_dev,
889 struct i2c_adapter *adapter,
890 const char *module_name, const char *client_type, u8 addr)
891{
892 unsigned short addrs[2] = { addr, I2C_CLIENT_END };
893
894 return v4l2_i2c_new_probed_subdev(v4l2_dev, adapter,
895 module_name, client_type, addrs);
896}
897EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev_addr);
898
Hans Verkuilf0222c72009-06-09 17:12:33 -0300899/* Load an i2c sub-device. */
900struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
901 struct i2c_adapter *adapter, const char *module_name,
902 struct i2c_board_info *info, const unsigned short *probe_addrs)
903{
904 struct v4l2_subdev *sd = NULL;
905 struct i2c_client *client;
906
907 BUG_ON(!v4l2_dev);
908
909 if (module_name)
910 request_module(module_name);
911
912 /* Create the i2c client */
913 if (info->addr == 0 && probe_addrs)
914 client = i2c_new_probed_device(adapter, info, probe_addrs);
915 else
916 client = i2c_new_device(adapter, info);
917
918 /* Note: by loading the module first we are certain that c->driver
919 will be set if the driver was found. If the module was not loaded
920 first, then the i2c core tries to delay-load the module for us,
921 and then c->driver is still NULL until the module is finally
922 loaded. This delay-load mechanism doesn't work if other drivers
923 want to use the i2c device, so explicitly loading the module
924 is the best alternative. */
925 if (client == NULL || client->driver == NULL)
926 goto error;
927
928 /* Lock the module so we can safely get the v4l2_subdev pointer */
929 if (!try_module_get(client->driver->driver.owner))
930 goto error;
931 sd = i2c_get_clientdata(client);
932
933 /* Register with the v4l2_device which increases the module's
934 use count as well. */
935 if (v4l2_device_register_subdev(v4l2_dev, sd))
936 sd = NULL;
937 /* Decrease the module use count to match the first try_module_get. */
938 module_put(client->driver->driver.owner);
939
940 if (sd) {
941 /* We return errors from v4l2_subdev_call only if we have the
942 callback as the .s_config is not mandatory */
943 int err = v4l2_subdev_call(sd, core, s_config,
944 info->irq, info->platform_data);
945
946 if (err && err != -ENOIOCTLCMD) {
947 v4l2_device_unregister_subdev(sd);
948 sd = NULL;
949 }
950 }
951
952error:
953 /* If we have a client but no subdev, then something went wrong and
954 we must unregister the client. */
955 if (client && sd == NULL)
956 i2c_unregister_device(client);
957 return sd;
958}
959EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
960
961struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev,
962 struct i2c_adapter *adapter,
963 const char *module_name, const char *client_type,
964 int irq, void *platform_data,
965 u8 addr, const unsigned short *probe_addrs)
966{
967 struct i2c_board_info info;
968
969 /* Setup the i2c board info with the device type and
970 the device address. */
971 memset(&info, 0, sizeof(info));
972 strlcpy(info.type, client_type, sizeof(info.type));
973 info.addr = addr;
974 info.irq = irq;
975 info.platform_data = platform_data;
976
977 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, module_name,
978 &info, probe_addrs);
979}
980EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg);
981
Hans Verkuilab3731902009-02-21 18:08:41 -0300982/* Return i2c client address of v4l2_subdev. */
983unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
984{
985 struct i2c_client *client = v4l2_get_subdevdata(sd);
986
987 return client ? client->addr : I2C_CLIENT_END;
988}
989EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
990
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300991/* Return a list of I2C tuner addresses to probe. Use only if the tuner
992 addresses are unknown. */
993const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
994{
995 static const unsigned short radio_addrs[] = {
996#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
997 0x10,
998#endif
999 0x60,
1000 I2C_CLIENT_END
1001 };
1002 static const unsigned short demod_addrs[] = {
1003 0x42, 0x43, 0x4a, 0x4b,
1004 I2C_CLIENT_END
1005 };
1006 static const unsigned short tv_addrs[] = {
1007 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Hans Verkuil416a7aa2009-05-02 09:42:57 -03001008 0x60, 0x61, 0x62, 0x63, 0x64,
Hans Verkuilc7d29e22009-01-18 19:37:59 -03001009 I2C_CLIENT_END
1010 };
1011
1012 switch (type) {
1013 case ADDRS_RADIO:
1014 return radio_addrs;
1015 case ADDRS_DEMOD:
1016 return demod_addrs;
1017 case ADDRS_TV:
1018 return tv_addrs;
1019 case ADDRS_TV_WITH_DEMOD:
1020 return tv_addrs + 4;
1021 }
1022 return NULL;
1023}
1024EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
1025
Trent Piephod8b29962009-06-12 16:31:29 -03001026#endif /* defined(CONFIG_I2C) */
1027
Trent Piephob0d31592009-05-30 21:45:46 -03001028/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
1029 * and max don't have to be aligned, but there must be at least one valid
1030 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
1031 * of 16 between 17 and 31. */
1032static unsigned int clamp_align(unsigned int x, unsigned int min,
1033 unsigned int max, unsigned int align)
1034{
1035 /* Bits that must be zero to be aligned */
1036 unsigned int mask = ~((1 << align) - 1);
1037
1038 /* Round to nearest aligned value */
1039 if (align)
1040 x = (x + (1 << (align - 1))) & mask;
1041
1042 /* Clamp to aligned value of min and max */
1043 if (x < min)
1044 x = (min + ~mask) & mask;
1045 else if (x > max)
1046 x = max & mask;
1047
1048 return x;
1049}
1050
1051/* Bound an image to have a width between wmin and wmax, and height between
1052 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
1053 * 2^walign, the height will be a multiple of 2^halign, and the overall size
1054 * (width*height) will be a multiple of 2^salign. The image may be shrunk
1055 * or enlarged to fit the alignment constraints.
1056 *
1057 * The width or height maximum must not be smaller than the corresponding
1058 * minimum. The alignments must not be so high there are no possible image
1059 * sizes within the allowed bounds. wmin and hmin must be at least 1
1060 * (don't use 0). If you don't care about a certain alignment, specify 0,
1061 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
1062 * you only want to adjust downward, specify a maximum that's the same as
1063 * the initial value.
1064 */
1065void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
1066 unsigned int walign,
1067 u32 *h, unsigned int hmin, unsigned int hmax,
1068 unsigned int halign, unsigned int salign)
1069{
1070 *w = clamp_align(*w, wmin, wmax, walign);
1071 *h = clamp_align(*h, hmin, hmax, halign);
1072
1073 /* Usually we don't need to align the size and are done now. */
1074 if (!salign)
1075 return;
1076
1077 /* How much alignment do we have? */
1078 walign = __ffs(*w);
1079 halign = __ffs(*h);
1080 /* Enough to satisfy the image alignment? */
1081 if (walign + halign < salign) {
1082 /* Max walign where there is still a valid width */
1083 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
1084 /* Max halign where there is still a valid height */
1085 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
1086
1087 /* up the smaller alignment until we have enough */
1088 do {
1089 if (halign >= hmaxa ||
1090 (walign <= halign && walign < wmaxa)) {
1091 *w = clamp_align(*w, wmin, wmax, walign + 1);
1092 walign = __ffs(*w);
1093 } else {
1094 *h = clamp_align(*h, hmin, hmax, halign + 1);
1095 halign = __ffs(*h);
1096 }
1097 } while (halign + walign < salign);
1098 }
1099}
1100EXPORT_SYMBOL_GPL(v4l_bound_align_image);