blob: b8f2be8d5c0ee3702bad5f880180797e711082f9 [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;
159 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
160 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
161 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
162 return 0;
163 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
164 return -ERANGE;
165 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
166 if (menu_items[ctrl->value] == NULL ||
167 menu_items[ctrl->value][0] == '\0')
168 return -EINVAL;
169 }
170 return 0;
171}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300172EXPORT_SYMBOL(v4l2_ctrl_check);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300173
174/* Returns NULL or a character pointer array containing the menu for
175 the given control ID. The pointer array ends with a NULL pointer.
176 An empty string signifies a menu entry that is invalid. This allows
177 drivers to disable certain options if it is not supported. */
178const char **v4l2_ctrl_get_menu(u32 id)
179{
180 static const char *mpeg_audio_sampling_freq[] = {
181 "44.1 kHz",
182 "48 kHz",
183 "32 kHz",
184 NULL
185 };
186 static const char *mpeg_audio_encoding[] = {
Hans Verkuile6b5da82008-08-08 07:38:07 -0300187 "MPEG-1/2 Layer I",
188 "MPEG-1/2 Layer II",
189 "MPEG-1/2 Layer III",
190 "MPEG-2/4 AAC",
191 "AC-3",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300192 NULL
193 };
194 static const char *mpeg_audio_l1_bitrate[] = {
195 "32 kbps",
196 "64 kbps",
197 "96 kbps",
198 "128 kbps",
199 "160 kbps",
200 "192 kbps",
201 "224 kbps",
202 "256 kbps",
203 "288 kbps",
204 "320 kbps",
205 "352 kbps",
206 "384 kbps",
207 "416 kbps",
208 "448 kbps",
209 NULL
210 };
211 static const char *mpeg_audio_l2_bitrate[] = {
212 "32 kbps",
213 "48 kbps",
214 "56 kbps",
215 "64 kbps",
216 "80 kbps",
217 "96 kbps",
218 "112 kbps",
219 "128 kbps",
220 "160 kbps",
221 "192 kbps",
222 "224 kbps",
223 "256 kbps",
224 "320 kbps",
225 "384 kbps",
226 NULL
227 };
228 static const char *mpeg_audio_l3_bitrate[] = {
229 "32 kbps",
230 "40 kbps",
231 "48 kbps",
232 "56 kbps",
233 "64 kbps",
234 "80 kbps",
235 "96 kbps",
236 "112 kbps",
237 "128 kbps",
238 "160 kbps",
239 "192 kbps",
240 "224 kbps",
241 "256 kbps",
242 "320 kbps",
243 NULL
244 };
Hans Verkuile6b5da82008-08-08 07:38:07 -0300245 static const char *mpeg_audio_ac3_bitrate[] = {
246 "32 kbps",
247 "40 kbps",
248 "48 kbps",
249 "56 kbps",
250 "64 kbps",
251 "80 kbps",
252 "96 kbps",
253 "112 kbps",
254 "128 kbps",
255 "160 kbps",
256 "192 kbps",
257 "224 kbps",
258 "256 kbps",
259 "320 kbps",
260 "384 kbps",
261 "448 kbps",
262 "512 kbps",
263 "576 kbps",
264 "640 kbps",
265 NULL
266 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300267 static const char *mpeg_audio_mode[] = {
268 "Stereo",
269 "Joint Stereo",
270 "Dual",
271 "Mono",
272 NULL
273 };
274 static const char *mpeg_audio_mode_extension[] = {
275 "Bound 4",
276 "Bound 8",
277 "Bound 12",
278 "Bound 16",
279 NULL
280 };
281 static const char *mpeg_audio_emphasis[] = {
282 "No Emphasis",
283 "50/15 us",
284 "CCITT J17",
285 NULL
286 };
287 static const char *mpeg_audio_crc[] = {
288 "No CRC",
289 "16-bit CRC",
290 NULL
291 };
292 static const char *mpeg_video_encoding[] = {
293 "MPEG-1",
294 "MPEG-2",
Janne Grunau188919a2008-08-08 07:21:00 -0300295 "MPEG-4 AVC",
Hans Verkuil9cb23182006-06-18 14:11:08 -0300296 NULL
297 };
298 static const char *mpeg_video_aspect[] = {
299 "1x1",
300 "4x3",
301 "16x9",
302 "2.21x1",
303 NULL
304 };
305 static const char *mpeg_video_bitrate_mode[] = {
306 "Variable Bitrate",
307 "Constant Bitrate",
308 NULL
309 };
310 static const char *mpeg_stream_type[] = {
311 "MPEG-2 Program Stream",
312 "MPEG-2 Transport Stream",
313 "MPEG-1 System Stream",
314 "MPEG-2 DVD-compatible Stream",
315 "MPEG-1 VCD-compatible Stream",
316 "MPEG-2 SVCD-compatible Stream",
317 NULL
318 };
Hans Verkuil8cbde942006-06-24 14:36:02 -0300319 static const char *mpeg_stream_vbi_fmt[] = {
320 "No VBI",
Hans Verkuil99523512006-06-25 11:18:54 -0300321 "Private packet, IVTV format",
Hans Verkuil8cbde942006-06-24 14:36:02 -0300322 NULL
323 };
Laurent Pinchart74980152008-12-14 16:24:04 -0300324 static const char *camera_power_line_frequency[] = {
325 "Disabled",
326 "50 Hz",
327 "60 Hz",
328 NULL
329 };
330 static const char *camera_exposure_auto[] = {
331 "Auto Mode",
332 "Manual Mode",
333 "Shutter Priority Mode",
334 "Aperture Priority Mode",
335 NULL
336 };
Hans Verkuil9cb23182006-06-18 14:11:08 -0300337
338 switch (id) {
339 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
340 return mpeg_audio_sampling_freq;
341 case V4L2_CID_MPEG_AUDIO_ENCODING:
342 return mpeg_audio_encoding;
343 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
344 return mpeg_audio_l1_bitrate;
345 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
346 return mpeg_audio_l2_bitrate;
347 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
348 return mpeg_audio_l3_bitrate;
Hans Verkuile6b5da82008-08-08 07:38:07 -0300349 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
350 return mpeg_audio_ac3_bitrate;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300351 case V4L2_CID_MPEG_AUDIO_MODE:
352 return mpeg_audio_mode;
353 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
354 return mpeg_audio_mode_extension;
355 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
356 return mpeg_audio_emphasis;
357 case V4L2_CID_MPEG_AUDIO_CRC:
358 return mpeg_audio_crc;
359 case V4L2_CID_MPEG_VIDEO_ENCODING:
360 return mpeg_video_encoding;
361 case V4L2_CID_MPEG_VIDEO_ASPECT:
362 return mpeg_video_aspect;
363 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
364 return mpeg_video_bitrate_mode;
365 case V4L2_CID_MPEG_STREAM_TYPE:
366 return mpeg_stream_type;
Hans Verkuil8cbde942006-06-24 14:36:02 -0300367 case V4L2_CID_MPEG_STREAM_VBI_FMT:
368 return mpeg_stream_vbi_fmt;
Laurent Pinchart74980152008-12-14 16:24:04 -0300369 case V4L2_CID_POWER_LINE_FREQUENCY:
370 return camera_power_line_frequency;
371 case V4L2_CID_EXPOSURE_AUTO:
372 return camera_exposure_auto;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300373 default:
374 return NULL;
375 }
376}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300377EXPORT_SYMBOL(v4l2_ctrl_get_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300378
Hans Verkuil69028d72008-08-08 07:55:00 -0300379/* Return the control name. */
380const char *v4l2_ctrl_get_name(u32 id)
381{
382 switch (id) {
383 /* USER controls */
Laurent Pinchart74980152008-12-14 16:24:04 -0300384 case V4L2_CID_USER_CLASS: return "User Controls";
385 case V4L2_CID_AUDIO_VOLUME: return "Volume";
386 case V4L2_CID_AUDIO_MUTE: return "Mute";
387 case V4L2_CID_AUDIO_BALANCE: return "Balance";
388 case V4L2_CID_AUDIO_BASS: return "Bass";
389 case V4L2_CID_AUDIO_TREBLE: return "Treble";
390 case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
391 case V4L2_CID_BRIGHTNESS: return "Brightness";
392 case V4L2_CID_CONTRAST: return "Contrast";
393 case V4L2_CID_SATURATION: return "Saturation";
394 case V4L2_CID_HUE: return "Hue";
395 case V4L2_CID_BLACK_LEVEL: return "Black Level";
396 case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
397 case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
398 case V4L2_CID_RED_BALANCE: return "Red Balance";
399 case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
400 case V4L2_CID_GAMMA: return "Gamma";
401 case V4L2_CID_EXPOSURE: return "Exposure";
402 case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
403 case V4L2_CID_GAIN: return "Gain";
404 case V4L2_CID_HFLIP: return "Horizontal Flip";
405 case V4L2_CID_VFLIP: return "Vertical Flip";
406 case V4L2_CID_HCENTER: return "Horizontal Center";
407 case V4L2_CID_VCENTER: return "Vertical Center";
408 case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
409 case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
410 case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
411 case V4L2_CID_SHARPNESS: return "Sharpness";
412 case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
413 case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
414 case V4L2_CID_COLOR_KILLER: return "Color Killer";
Hans Verkuil69028d72008-08-08 07:55:00 -0300415
416 /* MPEG controls */
417 case V4L2_CID_MPEG_CLASS: return "MPEG Encoder Controls";
418 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
419 case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
420 case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
421 case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
422 case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
Hans Verkuilf723af12008-08-09 10:35:29 -0300423 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
Hans Verkuil69028d72008-08-08 07:55:00 -0300424 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
425 case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
426 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
427 case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
428 case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
429 case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
430 case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
431 case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
432 case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
433 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
434 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
435 case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
436 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
437 case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
438 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
439 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
440 case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
441 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
442 case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
443 case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
444 case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
445 case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
446 case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
447 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
448 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
449 case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
450
Laurent Pinchart74980152008-12-14 16:24:04 -0300451 /* CAMERA controls */
452 case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
453 case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
454 case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
455 case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
456 case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
457 case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
458 case V4L2_CID_PAN_RESET: return "Pan, Reset";
459 case V4L2_CID_TILT_RESET: return "Tilt, Reset";
460 case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
461 case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
462 case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
463 case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
464 case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic";
465 case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
466 case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
467 case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
468 case V4L2_CID_PRIVACY: return "Privacy";
469
Hans Verkuil69028d72008-08-08 07:55:00 -0300470 default:
471 return NULL;
472 }
473}
474EXPORT_SYMBOL(v4l2_ctrl_get_name);
475
Hans Verkuil9cb23182006-06-18 14:11:08 -0300476/* Fill in a struct v4l2_queryctrl */
477int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
478{
Hans Verkuil69028d72008-08-08 07:55:00 -0300479 const char *name = v4l2_ctrl_get_name(qctrl->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300480
481 qctrl->flags = 0;
Hans Verkuil69028d72008-08-08 07:55:00 -0300482 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300483 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300484
Hans Verkuil9cb23182006-06-18 14:11:08 -0300485 switch (qctrl->id) {
486 case V4L2_CID_AUDIO_MUTE:
487 case V4L2_CID_AUDIO_LOUDNESS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300488 case V4L2_CID_AUTO_WHITE_BALANCE:
489 case V4L2_CID_AUTOGAIN:
490 case V4L2_CID_HFLIP:
491 case V4L2_CID_VFLIP:
492 case V4L2_CID_HUE_AUTO:
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300493 case V4L2_CID_MPEG_AUDIO_MUTE:
Hans Verkuil01f1e442007-08-21 18:32:42 -0300494 case V4L2_CID_MPEG_VIDEO_MUTE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300495 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
496 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
Laurent Pinchart74980152008-12-14 16:24:04 -0300497 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
498 case V4L2_CID_PRIVACY:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300499 qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
500 min = 0;
501 max = step = 1;
502 break;
Laurent Pinchart74980152008-12-14 16:24:04 -0300503 case V4L2_CID_POWER_LINE_FREQUENCY:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300504 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
505 case V4L2_CID_MPEG_AUDIO_ENCODING:
506 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
507 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
508 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
Hans Verkuile6b5da82008-08-08 07:38:07 -0300509 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300510 case V4L2_CID_MPEG_AUDIO_MODE:
511 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
512 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
513 case V4L2_CID_MPEG_AUDIO_CRC:
514 case V4L2_CID_MPEG_VIDEO_ENCODING:
515 case V4L2_CID_MPEG_VIDEO_ASPECT:
516 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
517 case V4L2_CID_MPEG_STREAM_TYPE:
Hans Verkuil8cbde942006-06-24 14:36:02 -0300518 case V4L2_CID_MPEG_STREAM_VBI_FMT:
Laurent Pinchart74980152008-12-14 16:24:04 -0300519 case V4L2_CID_EXPOSURE_AUTO:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300520 qctrl->type = V4L2_CTRL_TYPE_MENU;
521 step = 1;
522 break;
523 case V4L2_CID_USER_CLASS:
Laurent Pinchart74980152008-12-14 16:24:04 -0300524 case V4L2_CID_CAMERA_CLASS:
Hans Verkuil9cb23182006-06-18 14:11:08 -0300525 case V4L2_CID_MPEG_CLASS:
526 qctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
527 qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
528 min = max = step = def = 0;
529 break;
530 default:
531 qctrl->type = V4L2_CTRL_TYPE_INTEGER;
532 break;
533 }
534 switch (qctrl->id) {
535 case V4L2_CID_MPEG_AUDIO_ENCODING:
536 case V4L2_CID_MPEG_AUDIO_MODE:
537 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
538 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
539 case V4L2_CID_MPEG_STREAM_TYPE:
540 qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
541 break;
542 case V4L2_CID_AUDIO_VOLUME:
543 case V4L2_CID_AUDIO_BALANCE:
544 case V4L2_CID_AUDIO_BASS:
545 case V4L2_CID_AUDIO_TREBLE:
546 case V4L2_CID_BRIGHTNESS:
547 case V4L2_CID_CONTRAST:
548 case V4L2_CID_SATURATION:
549 case V4L2_CID_HUE:
550 qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
551 break;
552 }
553 qctrl->minimum = min;
554 qctrl->maximum = max;
555 qctrl->step = step;
556 qctrl->default_value = def;
557 qctrl->reserved[0] = qctrl->reserved[1] = 0;
558 snprintf(qctrl->name, sizeof(qctrl->name), name);
559 return 0;
560}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300561EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300562
563/* Fill in a struct v4l2_queryctrl with standard values based on
564 the control ID. */
565int v4l2_ctrl_query_fill_std(struct v4l2_queryctrl *qctrl)
566{
567 switch (qctrl->id) {
568 /* USER controls */
569 case V4L2_CID_USER_CLASS:
570 case V4L2_CID_MPEG_CLASS:
571 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
572 case V4L2_CID_AUDIO_VOLUME:
573 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 58880);
574 case V4L2_CID_AUDIO_MUTE:
575 case V4L2_CID_AUDIO_LOUDNESS:
576 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
577 case V4L2_CID_AUDIO_BALANCE:
578 case V4L2_CID_AUDIO_BASS:
579 case V4L2_CID_AUDIO_TREBLE:
580 return v4l2_ctrl_query_fill(qctrl, 0, 65535, 65535 / 100, 32768);
581 case V4L2_CID_BRIGHTNESS:
582 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
583 case V4L2_CID_CONTRAST:
584 case V4L2_CID_SATURATION:
585 return v4l2_ctrl_query_fill(qctrl, 0, 127, 1, 64);
586 case V4L2_CID_HUE:
587 return v4l2_ctrl_query_fill(qctrl, -128, 127, 1, 0);
588
589 /* MPEG controls */
590 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
591 return v4l2_ctrl_query_fill(qctrl,
592 V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100,
593 V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000, 1,
594 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
595 case V4L2_CID_MPEG_AUDIO_ENCODING:
596 return v4l2_ctrl_query_fill(qctrl,
597 V4L2_MPEG_AUDIO_ENCODING_LAYER_1,
Hans Verkuile6b5da82008-08-08 07:38:07 -0300598 V4L2_MPEG_AUDIO_ENCODING_AC3, 1,
Hans Verkuil9cb23182006-06-18 14:11:08 -0300599 V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
600 case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
601 return v4l2_ctrl_query_fill(qctrl,
602 V4L2_MPEG_AUDIO_L1_BITRATE_32K,
603 V4L2_MPEG_AUDIO_L1_BITRATE_448K, 1,
604 V4L2_MPEG_AUDIO_L1_BITRATE_256K);
605 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
606 return v4l2_ctrl_query_fill(qctrl,
607 V4L2_MPEG_AUDIO_L2_BITRATE_32K,
608 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
609 V4L2_MPEG_AUDIO_L2_BITRATE_224K);
610 case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
611 return v4l2_ctrl_query_fill(qctrl,
612 V4L2_MPEG_AUDIO_L3_BITRATE_32K,
613 V4L2_MPEG_AUDIO_L3_BITRATE_320K, 1,
614 V4L2_MPEG_AUDIO_L3_BITRATE_192K);
Hans Verkuilf723af12008-08-09 10:35:29 -0300615 case V4L2_CID_MPEG_AUDIO_AAC_BITRATE:
616 return v4l2_ctrl_query_fill(qctrl, 0, 6400, 1, 3200000);
Hans Verkuile6b5da82008-08-08 07:38:07 -0300617 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
618 return v4l2_ctrl_query_fill(qctrl,
619 V4L2_MPEG_AUDIO_AC3_BITRATE_32K,
620 V4L2_MPEG_AUDIO_AC3_BITRATE_640K, 1,
621 V4L2_MPEG_AUDIO_AC3_BITRATE_384K);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300622 case V4L2_CID_MPEG_AUDIO_MODE:
623 return v4l2_ctrl_query_fill(qctrl,
624 V4L2_MPEG_AUDIO_MODE_STEREO,
625 V4L2_MPEG_AUDIO_MODE_MONO, 1,
626 V4L2_MPEG_AUDIO_MODE_STEREO);
627 case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
628 return v4l2_ctrl_query_fill(qctrl,
629 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
630 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16, 1,
631 V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4);
632 case V4L2_CID_MPEG_AUDIO_EMPHASIS:
633 return v4l2_ctrl_query_fill(qctrl,
634 V4L2_MPEG_AUDIO_EMPHASIS_NONE,
635 V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17, 1,
636 V4L2_MPEG_AUDIO_EMPHASIS_NONE);
637 case V4L2_CID_MPEG_AUDIO_CRC:
638 return v4l2_ctrl_query_fill(qctrl,
639 V4L2_MPEG_AUDIO_CRC_NONE,
640 V4L2_MPEG_AUDIO_CRC_CRC16, 1,
641 V4L2_MPEG_AUDIO_CRC_NONE);
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300642 case V4L2_CID_MPEG_AUDIO_MUTE:
643 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300644 case V4L2_CID_MPEG_VIDEO_ENCODING:
645 return v4l2_ctrl_query_fill(qctrl,
646 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
Janne Grunau188919a2008-08-08 07:21:00 -0300647 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
Hans Verkuil9cb23182006-06-18 14:11:08 -0300648 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
649 case V4L2_CID_MPEG_VIDEO_ASPECT:
650 return v4l2_ctrl_query_fill(qctrl,
651 V4L2_MPEG_VIDEO_ASPECT_1x1,
652 V4L2_MPEG_VIDEO_ASPECT_221x100, 1,
653 V4L2_MPEG_VIDEO_ASPECT_4x3);
654 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
655 return v4l2_ctrl_query_fill(qctrl, 0, 33, 1, 2);
656 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
657 return v4l2_ctrl_query_fill(qctrl, 1, 34, 1, 12);
658 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
659 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
660 case V4L2_CID_MPEG_VIDEO_PULLDOWN:
661 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
662 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
663 return v4l2_ctrl_query_fill(qctrl,
664 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
665 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
666 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
667 case V4L2_CID_MPEG_VIDEO_BITRATE:
668 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
669 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
670 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
671 case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
672 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
Hans Verkuil5eee72e2007-04-27 12:31:00 -0300673 case V4L2_CID_MPEG_VIDEO_MUTE:
674 return v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 0);
675 case V4L2_CID_MPEG_VIDEO_MUTE_YUV: /* Init YUV (really YCbCr) to black */
676 return v4l2_ctrl_query_fill(qctrl, 0, 0xffffff, 1, 0x008080);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300677 case V4L2_CID_MPEG_STREAM_TYPE:
678 return v4l2_ctrl_query_fill(qctrl,
679 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
680 V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD, 1,
681 V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
682 case V4L2_CID_MPEG_STREAM_PID_PMT:
683 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
684 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
685 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
686 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
687 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
688 case V4L2_CID_MPEG_STREAM_PID_PCR:
689 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
690 case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO:
691 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
692 case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO:
693 return v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 0);
Hans Verkuil8cbde942006-06-24 14:36:02 -0300694 case V4L2_CID_MPEG_STREAM_VBI_FMT:
695 return v4l2_ctrl_query_fill(qctrl,
696 V4L2_MPEG_STREAM_VBI_FMT_NONE,
697 V4L2_MPEG_STREAM_VBI_FMT_IVTV, 1,
698 V4L2_MPEG_STREAM_VBI_FMT_NONE);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300699 default:
700 return -EINVAL;
701 }
702}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300703EXPORT_SYMBOL(v4l2_ctrl_query_fill_std);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300704
705/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300706 the menu. The qctrl pointer may be NULL, in which case it is ignored.
707 If menu_items is NULL, then the menu items are retrieved using
708 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300709int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
710 const char **menu_items)
711{
712 int i;
713
Hans Verkuil1e551262008-08-08 08:34:19 -0300714 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300715 if (menu_items == NULL)
716 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300717 if (menu_items == NULL ||
718 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
719 return -EINVAL;
720 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
721 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
722 return -EINVAL;
723 snprintf(qmenu->name, sizeof(qmenu->name), menu_items[qmenu->index]);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300724 return 0;
725}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300726EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300727
Hans Verkuil1e551262008-08-08 08:34:19 -0300728/* Fill in a struct v4l2_querymenu based on the specified array of valid
729 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
730 Use this if there are 'holes' in the list of valid menu items. */
731int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
732{
733 const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
734
735 qmenu->reserved = 0;
736 if (menu_items == NULL || ids == NULL)
737 return -EINVAL;
738 while (*ids != V4L2_CTRL_MENU_IDS_END) {
739 if (*ids++ == qmenu->index) {
740 snprintf(qmenu->name, sizeof(qmenu->name),
741 menu_items[qmenu->index]);
742 return 0;
743 }
744 }
745 return -EINVAL;
746}
747EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
748
Hans Verkuil9cb23182006-06-18 14:11:08 -0300749/* ctrl_classes points to an array of u32 pointers, the last element is
750 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
751 Each array must be sorted low to high and belong to the same control
752 class. The array of u32 pointer must also be sorted, from low class IDs
753 to high class IDs.
754
755 This function returns the first ID that follows after the given ID.
756 When no more controls are available 0 is returned. */
757u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
758{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300759 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300760 const u32 *pctrl;
761
Hans Verkuil9cb23182006-06-18 14:11:08 -0300762 if (ctrl_classes == NULL)
763 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300764
765 /* if no query is desired, then check if the ID is part of ctrl_classes */
766 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
767 /* find class */
768 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
769 ctrl_classes++;
770 if (*ctrl_classes == NULL)
771 return 0;
772 pctrl = *ctrl_classes;
773 /* find control ID */
774 while (*pctrl && *pctrl != id) pctrl++;
775 return *pctrl ? id : 0;
776 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300777 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300778 id++; /* select next control */
779 /* find first class that matches (or is greater than) the class of
780 the ID */
781 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
782 ctrl_classes++;
783 /* no more classes */
784 if (*ctrl_classes == NULL)
785 return 0;
786 pctrl = *ctrl_classes;
787 /* find first ctrl within the class that is >= ID */
788 while (*pctrl && *pctrl < id) pctrl++;
789 if (*pctrl)
790 return *pctrl;
791 /* we are at the end of the controls of the current class. */
792 /* continue with next class if available */
793 ctrl_classes++;
794 if (*ctrl_classes == NULL)
795 return 0;
796 return **ctrl_classes;
797}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300798EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300799
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300800int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300801{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300802 switch (match->type) {
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300803 case V4L2_CHIP_MATCH_HOST:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300804 return match->addr == 0;
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300805 default:
806 return 0;
807 }
808}
809EXPORT_SYMBOL(v4l2_chip_match_host);
810
811#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300812int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300813{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300814 int len;
815
816 if (c == NULL || match == NULL)
817 return 0;
818
819 switch (match->type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300820 case V4L2_CHIP_MATCH_I2C_DRIVER:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300821 if (c->driver == NULL || c->driver->driver.name == NULL)
822 return 0;
823 len = strlen(c->driver->driver.name);
824 /* legacy drivers have a ' suffix, don't try to match that */
825 if (len && c->driver->driver.name[len - 1] == '\'')
826 len--;
827 return len && !strncmp(c->driver->driver.name, match->name, len);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300828 case V4L2_CHIP_MATCH_I2C_ADDR:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300829 return c->addr == match->addr;
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300830 default:
831 return 0;
832 }
833}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300834EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300835
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300836int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
Hans Verkuil3434eb72007-04-27 12:31:08 -0300837 u32 ident, u32 revision)
838{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300839 if (!v4l2_chip_match_i2c_client(c, &chip->match))
Hans Verkuil3434eb72007-04-27 12:31:08 -0300840 return 0;
841 if (chip->ident == V4L2_IDENT_NONE) {
842 chip->ident = ident;
843 chip->revision = revision;
844 }
845 else {
846 chip->ident = V4L2_IDENT_AMBIGUOUS;
847 chip->revision = 0;
848 }
849 return 0;
850}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300851EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300852
Hans Verkuil9cb23182006-06-18 14:11:08 -0300853/* ----------------------------------------------------------------- */
854
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300855/* Helper function for I2C legacy drivers */
856
857int v4l2_i2c_attach(struct i2c_adapter *adapter, int address, struct i2c_driver *driver,
Jean Delvared2653e92008-04-29 23:11:39 +0200858 const char *name,
859 int (*probe)(struct i2c_client *, const struct i2c_device_id *))
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300860{
861 struct i2c_client *client;
862 int err;
863
864 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Harvey Harrisona6a3a172008-04-28 16:50:03 -0700865 if (!client)
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300866 return -ENOMEM;
867
868 client->addr = address;
869 client->adapter = adapter;
870 client->driver = driver;
Hans Verkuil393bf552007-09-18 03:22:32 -0300871 strlcpy(client->name, name, sizeof(client->name));
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300872
Jean Delvared2653e92008-04-29 23:11:39 +0200873 err = probe(client, NULL);
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300874 if (err == 0) {
875 i2c_attach_client(client);
Hans Verkuil393bf552007-09-18 03:22:32 -0300876 } else {
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300877 kfree(client);
878 }
Hans Verkuil188f3452007-09-16 10:47:15 -0300879 return err != -ENOMEM ? 0 : err;
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300880}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300881EXPORT_SYMBOL(v4l2_i2c_attach);
Hans Verkuildd991202008-11-23 12:19:45 -0300882
883void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
884 const struct v4l2_subdev_ops *ops)
885{
886 v4l2_subdev_init(sd, ops);
887 /* the owner is the same as the i2c_client's driver owner */
888 sd->owner = client->driver->driver.owner;
889 /* i2c_client and v4l2_subdev point to one another */
890 v4l2_set_subdevdata(sd, client);
891 i2c_set_clientdata(client, sd);
892 /* initialize name */
893 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
894 client->driver->driver.name, i2c_adapter_id(client->adapter),
895 client->addr);
896}
897EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
898
899
900
901/* Load an i2c sub-device. It assumes that i2c_get_adapdata(adapter)
902 returns the v4l2_device and that i2c_get_clientdata(client)
903 returns the v4l2_subdev. */
904struct v4l2_subdev *v4l2_i2c_new_subdev(struct i2c_adapter *adapter,
905 const char *module_name, const char *client_type, u8 addr)
906{
907 struct v4l2_device *dev = i2c_get_adapdata(adapter);
908 struct v4l2_subdev *sd = NULL;
909 struct i2c_client *client;
910 struct i2c_board_info info;
911
912 BUG_ON(!dev);
913#ifdef MODULE
914 if (module_name)
915 request_module(module_name);
916#endif
917 /* Setup the i2c board info with the device type and
918 the device address. */
919 memset(&info, 0, sizeof(info));
920 strlcpy(info.type, client_type, sizeof(info.type));
921 info.addr = addr;
922
923 /* Create the i2c client */
924 client = i2c_new_device(adapter, &info);
925 /* Note: it is possible in the future that
926 c->driver is NULL if the driver is still being loaded.
927 We need better support from the kernel so that we
928 can easily wait for the load to finish. */
929 if (client == NULL || client->driver == NULL)
930 return NULL;
931
932 /* Lock the module so we can safely get the v4l2_subdev pointer */
933 if (!try_module_get(client->driver->driver.owner))
934 return NULL;
935 sd = i2c_get_clientdata(client);
936
937 /* Register with the v4l2_device which increases the module's
938 use count as well. */
939 if (v4l2_device_register_subdev(dev, sd))
940 sd = NULL;
941 /* Decrease the module use count to match the first try_module_get. */
942 module_put(client->driver->driver.owner);
943 return sd;
944
945}
946EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
947
948/* Probe and load an i2c sub-device. It assumes that i2c_get_adapdata(adapter)
949 returns the v4l2_device and that i2c_get_clientdata(client)
950 returns the v4l2_subdev. */
951struct v4l2_subdev *v4l2_i2c_new_probed_subdev(struct i2c_adapter *adapter,
952 const char *module_name, const char *client_type,
953 const unsigned short *addrs)
954{
955 struct v4l2_device *dev = i2c_get_adapdata(adapter);
956 struct v4l2_subdev *sd = NULL;
957 struct i2c_client *client = NULL;
958 struct i2c_board_info info;
959
960 BUG_ON(!dev);
961#ifdef MODULE
962 if (module_name)
963 request_module(module_name);
964#endif
965 /* Setup the i2c board info with the device type and
966 the device address. */
967 memset(&info, 0, sizeof(info));
968 strlcpy(info.type, client_type, sizeof(info.type));
969
970 /* Probe and create the i2c client */
971 client = i2c_new_probed_device(adapter, &info, addrs);
972 /* Note: it is possible in the future that
973 c->driver is NULL if the driver is still being loaded.
974 We need better support from the kernel so that we
975 can easily wait for the load to finish. */
976 if (client == NULL || client->driver == NULL)
977 return NULL;
978
979 /* Lock the module so we can safely get the v4l2_subdev pointer */
980 if (!try_module_get(client->driver->driver.owner))
981 return NULL;
982 sd = i2c_get_clientdata(client);
983
984 /* Register with the v4l2_device which increases the module's
985 use count as well. */
986 if (v4l2_device_register_subdev(dev, sd))
987 sd = NULL;
988 /* Decrease the module use count to match the first try_module_get. */
989 module_put(client->driver->driver.owner);
990 return sd;
991}
992EXPORT_SYMBOL_GPL(v4l2_i2c_new_probed_subdev);
993
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300994#endif