blob: 9294282b5add27c04ada832d498f7f11aa0ef323 [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 Verkuil09965172010-08-01 14:32:42 -030065#include <media/v4l2-ctrls.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030066#include <media/v4l2-chip-ident.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Hans Verkuil33b687c2008-07-25 05:32:50 -030068#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
71MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
72MODULE_LICENSE("GPL");
73
74/*
75 *
76 * V 4 L 2 D R I V E R H E L P E R A P I
77 *
78 */
79
80/*
81 * Video Standard Operations (contributed by Michael Schimek)
82 */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* ----------------------------------------------------------------- */
86/* priority handling */
87
88#define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND || \
89 val == V4L2_PRIORITY_INTERACTIVE || \
90 val == V4L2_PRIORITY_RECORD)
91
Hans Verkuilffb48772010-05-01 08:03:24 -030092void v4l2_prio_init(struct v4l2_prio_state *global)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Hans Verkuilffb48772010-05-01 08:03:24 -030094 memset(global, 0, sizeof(*global));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
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
Hans Verkuilffb48772010-05-01 08:03:24 -0300114void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Hans Verkuilffb48772010-05-01 08:03:24 -0300116 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300118EXPORT_SYMBOL(v4l2_prio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Hans Verkuilffb48772010-05-01 08:03:24 -0300120void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Hans Verkuilffb48772010-05-01 08:03:24 -0300122 if (V4L2_PRIO_VALID(local))
123 atomic_dec(&global->prios[local]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300125EXPORT_SYMBOL(v4l2_prio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
128{
129 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
130 return V4L2_PRIORITY_RECORD;
131 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
132 return V4L2_PRIORITY_INTERACTIVE;
133 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
134 return V4L2_PRIORITY_BACKGROUND;
135 return V4L2_PRIORITY_UNSET;
136}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300137EXPORT_SYMBOL(v4l2_prio_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Hans Verkuilffb48772010-05-01 08:03:24 -0300139int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Hans Verkuilffb48772010-05-01 08:03:24 -0300141 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
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
Hans Verkuil9cb23182006-06-18 14:11:08 -0300176/* Fill in a struct v4l2_queryctrl */
177int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
178{
Hans Verkuil09965172010-08-01 14:32:42 -0300179 const char *name;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300180
Hans Verkuil09965172010-08-01 14:32:42 -0300181 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
182 &min, &max, &step, &def, &qctrl->flags);
183
Hans Verkuil69028d72008-08-08 07:55:00 -0300184 if (name == NULL)
Hans Verkuil9cb23182006-06-18 14:11:08 -0300185 return -EINVAL;
Hans Verkuil69028d72008-08-08 07:55:00 -0300186
Hans Verkuil9cb23182006-06-18 14:11:08 -0300187 qctrl->minimum = min;
188 qctrl->maximum = max;
189 qctrl->step = step;
190 qctrl->default_value = def;
191 qctrl->reserved[0] = qctrl->reserved[1] = 0;
Hans Verkuilb634a932009-01-17 11:26:59 -0300192 strlcpy(qctrl->name, name, sizeof(qctrl->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300193 return 0;
194}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300195EXPORT_SYMBOL(v4l2_ctrl_query_fill);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300196
Hans Verkuil9cb23182006-06-18 14:11:08 -0300197/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
Hans Verkuile281db582008-08-08 12:43:59 -0300198 the menu. The qctrl pointer may be NULL, in which case it is ignored.
199 If menu_items is NULL, then the menu items are retrieved using
200 v4l2_ctrl_get_menu. */
Hans Verkuil9cb23182006-06-18 14:11:08 -0300201int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
202 const char **menu_items)
203{
204 int i;
205
Hans Verkuil1e551262008-08-08 08:34:19 -0300206 qmenu->reserved = 0;
Hans Verkuile281db582008-08-08 12:43:59 -0300207 if (menu_items == NULL)
208 menu_items = v4l2_ctrl_get_menu(qmenu->id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300209 if (menu_items == NULL ||
210 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
211 return -EINVAL;
212 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
213 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
214 return -EINVAL;
Hans Verkuilb634a932009-01-17 11:26:59 -0300215 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
Hans Verkuil9cb23182006-06-18 14:11:08 -0300216 return 0;
217}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300218EXPORT_SYMBOL(v4l2_ctrl_query_menu);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300219
Hans Verkuil1e551262008-08-08 08:34:19 -0300220/* Fill in a struct v4l2_querymenu based on the specified array of valid
221 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
222 Use this if there are 'holes' in the list of valid menu items. */
223int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
224{
225 const char **menu_items = v4l2_ctrl_get_menu(qmenu->id);
226
227 qmenu->reserved = 0;
228 if (menu_items == NULL || ids == NULL)
229 return -EINVAL;
230 while (*ids != V4L2_CTRL_MENU_IDS_END) {
231 if (*ids++ == qmenu->index) {
Hans Verkuilb634a932009-01-17 11:26:59 -0300232 strlcpy(qmenu->name, menu_items[qmenu->index],
233 sizeof(qmenu->name));
Hans Verkuil1e551262008-08-08 08:34:19 -0300234 return 0;
235 }
236 }
237 return -EINVAL;
238}
239EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
240
Hans Verkuil9cb23182006-06-18 14:11:08 -0300241/* ctrl_classes points to an array of u32 pointers, the last element is
242 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
243 Each array must be sorted low to high and belong to the same control
Hans Verkuil2ba58892009-02-13 10:57:48 -0300244 class. The array of u32 pointers must also be sorted, from low class IDs
Hans Verkuil9cb23182006-06-18 14:11:08 -0300245 to high class IDs.
246
247 This function returns the first ID that follows after the given ID.
248 When no more controls are available 0 is returned. */
249u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
250{
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300251 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300252 const u32 *pctrl;
253
Hans Verkuil9cb23182006-06-18 14:11:08 -0300254 if (ctrl_classes == NULL)
255 return 0;
Hans Verkuila46c5fb2007-07-19 04:53:36 -0300256
257 /* if no query is desired, then check if the ID is part of ctrl_classes */
258 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
259 /* find class */
260 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
261 ctrl_classes++;
262 if (*ctrl_classes == NULL)
263 return 0;
264 pctrl = *ctrl_classes;
265 /* find control ID */
266 while (*pctrl && *pctrl != id) pctrl++;
267 return *pctrl ? id : 0;
268 }
Hans Verkuil9cb23182006-06-18 14:11:08 -0300269 id &= V4L2_CTRL_ID_MASK;
Hans Verkuil9cb23182006-06-18 14:11:08 -0300270 id++; /* select next control */
271 /* find first class that matches (or is greater than) the class of
272 the ID */
273 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
274 ctrl_classes++;
275 /* no more classes */
276 if (*ctrl_classes == NULL)
277 return 0;
278 pctrl = *ctrl_classes;
279 /* find first ctrl within the class that is >= ID */
280 while (*pctrl && *pctrl < id) pctrl++;
281 if (*pctrl)
282 return *pctrl;
283 /* we are at the end of the controls of the current class. */
284 /* continue with next class if available */
285 ctrl_classes++;
286 if (*ctrl_classes == NULL)
287 return 0;
288 return **ctrl_classes;
289}
Mauro Carvalho Chehab057596e2008-02-02 11:25:31 -0300290EXPORT_SYMBOL(v4l2_ctrl_next);
Hans Verkuil9cb23182006-06-18 14:11:08 -0300291
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300292int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300293{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300294 switch (match->type) {
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300295 case V4L2_CHIP_MATCH_HOST:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300296 return match->addr == 0;
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300297 default:
298 return 0;
299 }
300}
301EXPORT_SYMBOL(v4l2_chip_match_host);
302
303#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300304int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300305{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300306 int len;
307
308 if (c == NULL || match == NULL)
309 return 0;
310
311 switch (match->type) {
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300312 case V4L2_CHIP_MATCH_I2C_DRIVER:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300313 if (c->driver == NULL || c->driver->driver.name == NULL)
314 return 0;
315 len = strlen(c->driver->driver.name);
316 /* legacy drivers have a ' suffix, don't try to match that */
317 if (len && c->driver->driver.name[len - 1] == '\'')
318 len--;
319 return len && !strncmp(c->driver->driver.name, match->name, len);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300320 case V4L2_CHIP_MATCH_I2C_ADDR:
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300321 return c->addr == match->addr;
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300322 default:
323 return 0;
324 }
325}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300326EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300327
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300328int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
Hans Verkuil3434eb72007-04-27 12:31:08 -0300329 u32 ident, u32 revision)
330{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300331 if (!v4l2_chip_match_i2c_client(c, &chip->match))
Hans Verkuil3434eb72007-04-27 12:31:08 -0300332 return 0;
333 if (chip->ident == V4L2_IDENT_NONE) {
334 chip->ident = ident;
335 chip->revision = revision;
336 }
337 else {
338 chip->ident = V4L2_IDENT_AMBIGUOUS;
339 chip->revision = 0;
340 }
341 return 0;
342}
Mauro Carvalho Chehaba9254472008-01-29 18:32:35 -0300343EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
Hans Verkuilf3d092b2007-02-23 20:55:14 -0300344
Hans Verkuil9cb23182006-06-18 14:11:08 -0300345/* ----------------------------------------------------------------- */
346
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300347/* I2C Helper functions */
Hans Verkuil8ffbc652007-09-12 08:32:50 -0300348
Hans Verkuildd991202008-11-23 12:19:45 -0300349
350void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
351 const struct v4l2_subdev_ops *ops)
352{
353 v4l2_subdev_init(sd, ops);
Hans Verkuilb5b2b7e2009-05-02 10:58:51 -0300354 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
Hans Verkuildd991202008-11-23 12:19:45 -0300355 /* the owner is the same as the i2c_client's driver owner */
356 sd->owner = client->driver->driver.owner;
357 /* i2c_client and v4l2_subdev point to one another */
358 v4l2_set_subdevdata(sd, client);
359 i2c_set_clientdata(client, sd);
360 /* initialize name */
361 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
362 client->driver->driver.name, i2c_adapter_id(client->adapter),
363 client->addr);
364}
365EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
366
367
368
Hans Verkuile6574f22009-04-01 03:57:53 -0300369/* Load an i2c sub-device. */
Hans Verkuilf0222c72009-06-09 17:12:33 -0300370struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
371 struct i2c_adapter *adapter, const char *module_name,
372 struct i2c_board_info *info, const unsigned short *probe_addrs)
373{
374 struct v4l2_subdev *sd = NULL;
375 struct i2c_client *client;
376
377 BUG_ON(!v4l2_dev);
378
379 if (module_name)
380 request_module(module_name);
Laurent Pincharte9f04952010-09-24 07:37:47 -0300381 else
382 request_module(I2C_MODULE_PREFIX "%s", info->type);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300383
384 /* Create the i2c client */
385 if (info->addr == 0 && probe_addrs)
Jean Delvare9a942412010-08-11 18:20:56 +0200386 client = i2c_new_probed_device(adapter, info, probe_addrs,
387 NULL);
Hans Verkuilf0222c72009-06-09 17:12:33 -0300388 else
389 client = i2c_new_device(adapter, info);
390
391 /* Note: by loading the module first we are certain that c->driver
392 will be set if the driver was found. If the module was not loaded
393 first, then the i2c core tries to delay-load the module for us,
394 and then c->driver is still NULL until the module is finally
395 loaded. This delay-load mechanism doesn't work if other drivers
396 want to use the i2c device, so explicitly loading the module
397 is the best alternative. */
398 if (client == NULL || client->driver == NULL)
399 goto error;
400
401 /* Lock the module so we can safely get the v4l2_subdev pointer */
402 if (!try_module_get(client->driver->driver.owner))
403 goto error;
404 sd = i2c_get_clientdata(client);
405
406 /* Register with the v4l2_device which increases the module's
407 use count as well. */
408 if (v4l2_device_register_subdev(v4l2_dev, sd))
409 sd = NULL;
410 /* Decrease the module use count to match the first try_module_get. */
411 module_put(client->driver->driver.owner);
412
413 if (sd) {
414 /* We return errors from v4l2_subdev_call only if we have the
415 callback as the .s_config is not mandatory */
416 int err = v4l2_subdev_call(sd, core, s_config,
417 info->irq, info->platform_data);
418
419 if (err && err != -ENOIOCTLCMD) {
420 v4l2_device_unregister_subdev(sd);
421 sd = NULL;
422 }
423 }
424
425error:
426 /* If we have a client but no subdev, then something went wrong and
427 we must unregister the client. */
428 if (client && sd == NULL)
429 i2c_unregister_device(client);
430 return sd;
431}
432EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
433
434struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev,
435 struct i2c_adapter *adapter,
436 const char *module_name, const char *client_type,
437 int irq, void *platform_data,
438 u8 addr, const unsigned short *probe_addrs)
439{
440 struct i2c_board_info info;
441
442 /* Setup the i2c board info with the device type and
443 the device address. */
444 memset(&info, 0, sizeof(info));
445 strlcpy(info.type, client_type, sizeof(info.type));
446 info.addr = addr;
447 info.irq = irq;
448 info.platform_data = platform_data;
449
450 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, module_name,
451 &info, probe_addrs);
452}
453EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg);
454
Hans Verkuilab373192009-02-21 18:08:41 -0300455/* Return i2c client address of v4l2_subdev. */
456unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
457{
458 struct i2c_client *client = v4l2_get_subdevdata(sd);
459
460 return client ? client->addr : I2C_CLIENT_END;
461}
462EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
463
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300464/* Return a list of I2C tuner addresses to probe. Use only if the tuner
465 addresses are unknown. */
466const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
467{
468 static const unsigned short radio_addrs[] = {
469#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
470 0x10,
471#endif
472 0x60,
473 I2C_CLIENT_END
474 };
475 static const unsigned short demod_addrs[] = {
476 0x42, 0x43, 0x4a, 0x4b,
477 I2C_CLIENT_END
478 };
479 static const unsigned short tv_addrs[] = {
480 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Hans Verkuil416a7aa2009-05-02 09:42:57 -0300481 0x60, 0x61, 0x62, 0x63, 0x64,
Hans Verkuilc7d29e22009-01-18 19:37:59 -0300482 I2C_CLIENT_END
483 };
484
485 switch (type) {
486 case ADDRS_RADIO:
487 return radio_addrs;
488 case ADDRS_DEMOD:
489 return demod_addrs;
490 case ADDRS_TV:
491 return tv_addrs;
492 case ADDRS_TV_WITH_DEMOD:
493 return tv_addrs + 4;
494 }
495 return NULL;
496}
497EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
498
Trent Piephod8b29962009-06-12 16:31:29 -0300499#endif /* defined(CONFIG_I2C) */
500
Dmitri Belimov85e09212010-03-23 11:23:29 -0300501#if defined(CONFIG_SPI)
502
503/* Load a spi sub-device. */
504
505void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
506 const struct v4l2_subdev_ops *ops)
507{
508 v4l2_subdev_init(sd, ops);
509 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
510 /* the owner is the same as the spi_device's driver owner */
511 sd->owner = spi->dev.driver->owner;
512 /* spi_device and v4l2_subdev point to one another */
513 v4l2_set_subdevdata(sd, spi);
514 spi_set_drvdata(spi, sd);
515 /* initialize name */
516 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
517}
518EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
519
520struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
521 struct spi_master *master, struct spi_board_info *info)
522{
523 struct v4l2_subdev *sd = NULL;
524 struct spi_device *spi = NULL;
525
526 BUG_ON(!v4l2_dev);
527
528 if (info->modalias)
529 request_module(info->modalias);
530
531 spi = spi_new_device(master, info);
532
533 if (spi == NULL || spi->dev.driver == NULL)
534 goto error;
535
536 if (!try_module_get(spi->dev.driver->owner))
537 goto error;
538
539 sd = spi_get_drvdata(spi);
540
541 /* Register with the v4l2_device which increases the module's
542 use count as well. */
543 if (v4l2_device_register_subdev(v4l2_dev, sd))
544 sd = NULL;
545
546 /* Decrease the module use count to match the first try_module_get. */
547 module_put(spi->dev.driver->owner);
548
549error:
550 /* If we have a client but no subdev, then something went wrong and
551 we must unregister the client. */
552 if (spi && sd == NULL)
553 spi_unregister_device(spi);
554
555 return sd;
556}
557EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
558
559#endif /* defined(CONFIG_SPI) */
560
Trent Piephob0d31592009-05-30 21:45:46 -0300561/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
562 * and max don't have to be aligned, but there must be at least one valid
563 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
564 * of 16 between 17 and 31. */
565static unsigned int clamp_align(unsigned int x, unsigned int min,
566 unsigned int max, unsigned int align)
567{
568 /* Bits that must be zero to be aligned */
569 unsigned int mask = ~((1 << align) - 1);
570
571 /* Round to nearest aligned value */
572 if (align)
573 x = (x + (1 << (align - 1))) & mask;
574
575 /* Clamp to aligned value of min and max */
576 if (x < min)
577 x = (min + ~mask) & mask;
578 else if (x > max)
579 x = max & mask;
580
581 return x;
582}
583
584/* Bound an image to have a width between wmin and wmax, and height between
585 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
586 * 2^walign, the height will be a multiple of 2^halign, and the overall size
587 * (width*height) will be a multiple of 2^salign. The image may be shrunk
588 * or enlarged to fit the alignment constraints.
589 *
590 * The width or height maximum must not be smaller than the corresponding
591 * minimum. The alignments must not be so high there are no possible image
592 * sizes within the allowed bounds. wmin and hmin must be at least 1
593 * (don't use 0). If you don't care about a certain alignment, specify 0,
594 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
595 * you only want to adjust downward, specify a maximum that's the same as
596 * the initial value.
597 */
598void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
599 unsigned int walign,
600 u32 *h, unsigned int hmin, unsigned int hmax,
601 unsigned int halign, unsigned int salign)
602{
603 *w = clamp_align(*w, wmin, wmax, walign);
604 *h = clamp_align(*h, hmin, hmax, halign);
605
606 /* Usually we don't need to align the size and are done now. */
607 if (!salign)
608 return;
609
610 /* How much alignment do we have? */
611 walign = __ffs(*w);
612 halign = __ffs(*h);
613 /* Enough to satisfy the image alignment? */
614 if (walign + halign < salign) {
615 /* Max walign where there is still a valid width */
616 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
617 /* Max halign where there is still a valid height */
618 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
619
620 /* up the smaller alignment until we have enough */
621 do {
622 if (halign >= hmaxa ||
623 (walign <= halign && walign < wmaxa)) {
624 *w = clamp_align(*w, wmin, wmax, walign + 1);
625 walign = __ffs(*w);
626 } else {
627 *h = clamp_align(*h, hmin, hmax, halign + 1);
628 halign = __ffs(*h);
629 }
630 } while (halign + walign < salign);
631 }
632}
633EXPORT_SYMBOL_GPL(v4l_bound_align_image);
Muralidharan Karicheri2e535ed2009-12-10 04:39:47 -0300634
635/**
636 * v4l_fill_dv_preset_info - fill description of a digital video preset
637 * @preset - preset value
638 * @info - pointer to struct v4l2_dv_enum_preset
639 *
640 * drivers can use this helper function to fill description of dv preset
641 * in info.
642 */
643int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
644{
645 static const struct v4l2_dv_preset_info {
646 u16 width;
647 u16 height;
648 const char *name;
649 } dv_presets[] = {
650 { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */
651 { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */
652 { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */
653 { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */
654 { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */
655 { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */
656 { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */
657 { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */
658 { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */
659 { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */
660 { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */
661 { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */
662 { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */
663 { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */
664 { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */
665 { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */
666 { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */
667 { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */
668 { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */
669 };
670
671 if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
672 return -EINVAL;
673
674 info->preset = preset;
675 info->width = dv_presets[preset].width;
676 info->height = dv_presets[preset].height;
677 strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
678 return 0;
679}
680EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300681
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300682const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
683 const struct v4l2_discrete_probe *probe,
684 s32 width, s32 height)
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300685{
686 int i;
687 u32 error, min_error = UINT_MAX;
Hans Verkuil3fd8e642010-09-30 09:29:37 -0300688 const struct v4l2_frmsize_discrete *size, *best = NULL;
Guennadi Liakhovetski79c6ff92010-08-27 13:41:44 -0300689
690 if (!probe)
691 return best;
692
693 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
694 error = abs(size->width - width) + abs(size->height - height);
695 if (error < min_error) {
696 min_error = error;
697 best = size;
698 }
699 if (!error)
700 break;
701 }
702
703 return best;
704}
705EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);