[media] v4l2-ctrls: use const char * const * for the menu arrays
[deliverable/linux.git] / drivers / media / video / v4l2-common.c
CommitLineData
1da177e4
LT
1/*
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 *
43db48d3 15 * Author: Bill Dirks <bill@thedirks.org>
1da177e4
LT
16 * 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 *
d9b01449 31 * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
32 *
33 * Fixes:
34 */
35
36/*
37 * Video4linux 1/2 integration by Justin Schoeman
38 * <justin@suntiger.ee.up.ac.za>
39 * 2.4 PROCFS support ported from 2.4 kernels by
96de0e25 40 * Iñaki García Etxebarria <garetxe@euskalnet.net>
1da177e4
LT
41 * 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
1da177e4
LT
47#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
1da177e4
LT
50#include <linux/mm.h>
51#include <linux/string.h>
52#include <linux/errno.h>
f3d092b8 53#include <linux/i2c.h>
85e09219
DB
54#if defined(CONFIG_SPI)
55#include <linux/spi/spi.h>
56#endif
1da177e4
LT
57#include <asm/uaccess.h>
58#include <asm/system.h>
59#include <asm/pgtable.h>
60#include <asm/io.h>
61#include <asm/div64.h>
401998fa 62#define __OLD_VIDIOC_ /* To allow fixing old calls*/
5e453dc7 63#include <media/v4l2-common.h>
dd99120c 64#include <media/v4l2-device.h>
0996517c 65#include <media/v4l2-ctrls.h>
3434eb7e 66#include <media/v4l2-chip-ident.h>
1da177e4 67
33b687cf 68#include <linux/videodev2.h>
1da177e4
LT
69
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
1da177e4 84
1da177e4
LT
85/* ----------------------------------------------------------------- */
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
ffb4877b 92void v4l2_prio_init(struct v4l2_prio_state *global)
1da177e4 93{
ffb4877b 94 memset(global, 0, sizeof(*global));
1da177e4 95}
057596ee 96EXPORT_SYMBOL(v4l2_prio_init);
1da177e4
LT
97
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}
057596ee 112EXPORT_SYMBOL(v4l2_prio_change);
1da177e4 113
ffb4877b 114void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
1da177e4 115{
ffb4877b 116 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
1da177e4 117}
057596ee 118EXPORT_SYMBOL(v4l2_prio_open);
1da177e4 119
ffb4877b 120void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
1da177e4 121{
ffb4877b
HV
122 if (V4L2_PRIO_VALID(local))
123 atomic_dec(&global->prios[local]);
1da177e4 124}
057596ee 125EXPORT_SYMBOL(v4l2_prio_close);
1da177e4
LT
126
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}
057596ee 137EXPORT_SYMBOL(v4l2_prio_max);
1da177e4 138
ffb4877b 139int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
1da177e4 140{
ffb4877b 141 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
1da177e4 142}
057596ee 143EXPORT_SYMBOL(v4l2_prio_check);
1da177e4
LT
144
145/* ----------------------------------------------------------------- */
146
9cb2318b
HV
147/* 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,
513521ea 153 const char * const *menu_items)
9cb2318b
HV
154{
155 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
156 return -EINVAL;
157 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
158 return -EBUSY;
6b5a9492
HV
159 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
160 return 0;
9cb2318b
HV
161 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}
057596ee 174EXPORT_SYMBOL(v4l2_ctrl_check);
9cb2318b 175
69028d70
HV
176/* Fill in a struct v4l2_queryctrl */
177int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
178{
0996517c
HV
179 const char *name;
180
181 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
182 &min, &max, &step, &def, &qctrl->flags);
69028d70 183
69028d70
HV
184 if (name == NULL)
185 return -EINVAL;
186
9cb2318b
HV
187 qctrl->minimum = min;
188 qctrl->maximum = max;
189 qctrl->step = step;
190 qctrl->default_value = def;
191 qctrl->reserved[0] = qctrl->reserved[1] = 0;
b634a93f 192 strlcpy(qctrl->name, name, sizeof(qctrl->name));
9cb2318b
HV
193 return 0;
194}
057596ee 195EXPORT_SYMBOL(v4l2_ctrl_query_fill);
9cb2318b 196
9cb2318b 197/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
e281db58
HV
198 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. */
9cb2318b 201int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
513521ea 202 const char * const *menu_items)
9cb2318b
HV
203{
204 int i;
205
1e551266 206 qmenu->reserved = 0;
e281db58
HV
207 if (menu_items == NULL)
208 menu_items = v4l2_ctrl_get_menu(qmenu->id);
9cb2318b
HV
209 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;
b634a93f 215 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
9cb2318b
HV
216 return 0;
217}
057596ee 218EXPORT_SYMBOL(v4l2_ctrl_query_menu);
9cb2318b 219
1e551266
HV
220/* 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{
513521ea 225 const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
1e551266
HV
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) {
b634a93f
HV
232 strlcpy(qmenu->name, menu_items[qmenu->index],
233 sizeof(qmenu->name));
1e551266
HV
234 return 0;
235 }
236 }
237 return -EINVAL;
238}
239EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
240
9cb2318b
HV
241/* 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
2ba58894 244 class. The array of u32 pointers must also be sorted, from low class IDs
9cb2318b
HV
245 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{
a46c5fbc 251 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
9cb2318b
HV
252 const u32 *pctrl;
253
9cb2318b
HV
254 if (ctrl_classes == NULL)
255 return 0;
a46c5fbc
HV
256
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 }
9cb2318b 269 id &= V4L2_CTRL_ID_MASK;
9cb2318b
HV
270 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}
057596ee 290EXPORT_SYMBOL(v4l2_ctrl_next);
9cb2318b 291
aecde8b5 292int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
a9254475 293{
aecde8b5 294 switch (match->type) {
a9254475 295 case V4L2_CHIP_MATCH_HOST:
aecde8b5 296 return match->addr == 0;
a9254475
MCC
297 default:
298 return 0;
299 }
300}
301EXPORT_SYMBOL(v4l2_chip_match_host);
302
303#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
aecde8b5 304int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
f3d092b8 305{
aecde8b5
HV
306 int len;
307
308 if (c == NULL || match == NULL)
309 return 0;
310
311 switch (match->type) {
f3d092b8 312 case V4L2_CHIP_MATCH_I2C_DRIVER:
aecde8b5
HV
313 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);
f3d092b8 320 case V4L2_CHIP_MATCH_I2C_ADDR:
aecde8b5 321 return c->addr == match->addr;
f3d092b8
HV
322 default:
323 return 0;
324 }
325}
a9254475 326EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
f3d092b8 327
aecde8b5 328int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
3434eb7e
HV
329 u32 ident, u32 revision)
330{
aecde8b5 331 if (!v4l2_chip_match_i2c_client(c, &chip->match))
3434eb7e
HV
332 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}
a9254475 343EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
f3d092b8 344
9cb2318b
HV
345/* ----------------------------------------------------------------- */
346
78a3b4db 347/* I2C Helper functions */
8ffbc655 348
dd99120c
HV
349
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);
b5b2b7ed 354 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
dd99120c
HV
355 /* 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
f0222c7d
HV
369/* Load an i2c sub-device. */
370struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
9a1f8b34
LP
371 struct i2c_adapter *adapter, struct i2c_board_info *info,
372 const unsigned short *probe_addrs)
f0222c7d
HV
373{
374 struct v4l2_subdev *sd = NULL;
375 struct i2c_client *client;
376
377 BUG_ON(!v4l2_dev);
378
9a1f8b34 379 request_module(I2C_MODULE_PREFIX "%s", info->type);
f0222c7d
HV
380
381 /* Create the i2c client */
382 if (info->addr == 0 && probe_addrs)
9a94241a
JD
383 client = i2c_new_probed_device(adapter, info, probe_addrs,
384 NULL);
f0222c7d
HV
385 else
386 client = i2c_new_device(adapter, info);
387
388 /* Note: by loading the module first we are certain that c->driver
389 will be set if the driver was found. If the module was not loaded
390 first, then the i2c core tries to delay-load the module for us,
391 and then c->driver is still NULL until the module is finally
392 loaded. This delay-load mechanism doesn't work if other drivers
393 want to use the i2c device, so explicitly loading the module
394 is the best alternative. */
395 if (client == NULL || client->driver == NULL)
396 goto error;
397
398 /* Lock the module so we can safely get the v4l2_subdev pointer */
399 if (!try_module_get(client->driver->driver.owner))
400 goto error;
401 sd = i2c_get_clientdata(client);
402
403 /* Register with the v4l2_device which increases the module's
404 use count as well. */
405 if (v4l2_device_register_subdev(v4l2_dev, sd))
406 sd = NULL;
407 /* Decrease the module use count to match the first try_module_get. */
408 module_put(client->driver->driver.owner);
409
410 if (sd) {
411 /* We return errors from v4l2_subdev_call only if we have the
412 callback as the .s_config is not mandatory */
413 int err = v4l2_subdev_call(sd, core, s_config,
414 info->irq, info->platform_data);
415
416 if (err && err != -ENOIOCTLCMD) {
417 v4l2_device_unregister_subdev(sd);
418 sd = NULL;
419 }
420 }
421
422error:
423 /* If we have a client but no subdev, then something went wrong and
424 we must unregister the client. */
425 if (client && sd == NULL)
426 i2c_unregister_device(client);
427 return sd;
428}
429EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
430
431struct v4l2_subdev *v4l2_i2c_new_subdev_cfg(struct v4l2_device *v4l2_dev,
9a1f8b34 432 struct i2c_adapter *adapter, const char *client_type,
f0222c7d
HV
433 int irq, void *platform_data,
434 u8 addr, const unsigned short *probe_addrs)
435{
436 struct i2c_board_info info;
437
438 /* Setup the i2c board info with the device type and
439 the device address. */
440 memset(&info, 0, sizeof(info));
441 strlcpy(info.type, client_type, sizeof(info.type));
442 info.addr = addr;
443 info.irq = irq;
444 info.platform_data = platform_data;
445
9a1f8b34 446 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
f0222c7d
HV
447}
448EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_cfg);
449
ab373190
HV
450/* Return i2c client address of v4l2_subdev. */
451unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
452{
453 struct i2c_client *client = v4l2_get_subdevdata(sd);
454
455 return client ? client->addr : I2C_CLIENT_END;
456}
457EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
458
c7d29e2f
HV
459/* Return a list of I2C tuner addresses to probe. Use only if the tuner
460 addresses are unknown. */
461const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
462{
463 static const unsigned short radio_addrs[] = {
464#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
465 0x10,
466#endif
467 0x60,
468 I2C_CLIENT_END
469 };
470 static const unsigned short demod_addrs[] = {
471 0x42, 0x43, 0x4a, 0x4b,
472 I2C_CLIENT_END
473 };
474 static const unsigned short tv_addrs[] = {
475 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
416a7aa8 476 0x60, 0x61, 0x62, 0x63, 0x64,
c7d29e2f
HV
477 I2C_CLIENT_END
478 };
479
480 switch (type) {
481 case ADDRS_RADIO:
482 return radio_addrs;
483 case ADDRS_DEMOD:
484 return demod_addrs;
485 case ADDRS_TV:
486 return tv_addrs;
487 case ADDRS_TV_WITH_DEMOD:
488 return tv_addrs + 4;
489 }
490 return NULL;
491}
492EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
493
d8b29966
TP
494#endif /* defined(CONFIG_I2C) */
495
85e09219
DB
496#if defined(CONFIG_SPI)
497
498/* Load a spi sub-device. */
499
500void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
501 const struct v4l2_subdev_ops *ops)
502{
503 v4l2_subdev_init(sd, ops);
504 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
505 /* the owner is the same as the spi_device's driver owner */
506 sd->owner = spi->dev.driver->owner;
507 /* spi_device and v4l2_subdev point to one another */
508 v4l2_set_subdevdata(sd, spi);
509 spi_set_drvdata(spi, sd);
510 /* initialize name */
511 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
512}
513EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
514
515struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
516 struct spi_master *master, struct spi_board_info *info)
517{
518 struct v4l2_subdev *sd = NULL;
519 struct spi_device *spi = NULL;
520
521 BUG_ON(!v4l2_dev);
522
523 if (info->modalias)
524 request_module(info->modalias);
525
526 spi = spi_new_device(master, info);
527
528 if (spi == NULL || spi->dev.driver == NULL)
529 goto error;
530
531 if (!try_module_get(spi->dev.driver->owner))
532 goto error;
533
534 sd = spi_get_drvdata(spi);
535
536 /* Register with the v4l2_device which increases the module's
537 use count as well. */
538 if (v4l2_device_register_subdev(v4l2_dev, sd))
539 sd = NULL;
540
541 /* Decrease the module use count to match the first try_module_get. */
542 module_put(spi->dev.driver->owner);
543
544error:
545 /* If we have a client but no subdev, then something went wrong and
546 we must unregister the client. */
547 if (spi && sd == NULL)
548 spi_unregister_device(spi);
549
550 return sd;
551}
552EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
553
554#endif /* defined(CONFIG_SPI) */
555
b0d3159b
TP
556/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
557 * and max don't have to be aligned, but there must be at least one valid
558 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
559 * of 16 between 17 and 31. */
560static unsigned int clamp_align(unsigned int x, unsigned int min,
561 unsigned int max, unsigned int align)
562{
563 /* Bits that must be zero to be aligned */
564 unsigned int mask = ~((1 << align) - 1);
565
566 /* Round to nearest aligned value */
567 if (align)
568 x = (x + (1 << (align - 1))) & mask;
569
570 /* Clamp to aligned value of min and max */
571 if (x < min)
572 x = (min + ~mask) & mask;
573 else if (x > max)
574 x = max & mask;
575
576 return x;
577}
578
579/* Bound an image to have a width between wmin and wmax, and height between
580 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
581 * 2^walign, the height will be a multiple of 2^halign, and the overall size
582 * (width*height) will be a multiple of 2^salign. The image may be shrunk
583 * or enlarged to fit the alignment constraints.
584 *
585 * The width or height maximum must not be smaller than the corresponding
586 * minimum. The alignments must not be so high there are no possible image
587 * sizes within the allowed bounds. wmin and hmin must be at least 1
588 * (don't use 0). If you don't care about a certain alignment, specify 0,
589 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
590 * you only want to adjust downward, specify a maximum that's the same as
591 * the initial value.
592 */
593void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
594 unsigned int walign,
595 u32 *h, unsigned int hmin, unsigned int hmax,
596 unsigned int halign, unsigned int salign)
597{
598 *w = clamp_align(*w, wmin, wmax, walign);
599 *h = clamp_align(*h, hmin, hmax, halign);
600
601 /* Usually we don't need to align the size and are done now. */
602 if (!salign)
603 return;
604
605 /* How much alignment do we have? */
606 walign = __ffs(*w);
607 halign = __ffs(*h);
608 /* Enough to satisfy the image alignment? */
609 if (walign + halign < salign) {
610 /* Max walign where there is still a valid width */
611 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
612 /* Max halign where there is still a valid height */
613 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
614
615 /* up the smaller alignment until we have enough */
616 do {
617 if (halign >= hmaxa ||
618 (walign <= halign && walign < wmaxa)) {
619 *w = clamp_align(*w, wmin, wmax, walign + 1);
620 walign = __ffs(*w);
621 } else {
622 *h = clamp_align(*h, hmin, hmax, halign + 1);
623 halign = __ffs(*h);
624 }
625 } while (halign + walign < salign);
626 }
627}
628EXPORT_SYMBOL_GPL(v4l_bound_align_image);
2e535ed5
MK
629
630/**
631 * v4l_fill_dv_preset_info - fill description of a digital video preset
632 * @preset - preset value
633 * @info - pointer to struct v4l2_dv_enum_preset
634 *
635 * drivers can use this helper function to fill description of dv preset
636 * in info.
637 */
638int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
639{
640 static const struct v4l2_dv_preset_info {
641 u16 width;
642 u16 height;
643 const char *name;
644 } dv_presets[] = {
645 { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */
646 { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */
647 { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */
648 { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */
649 { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */
650 { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */
651 { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */
652 { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */
653 { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */
654 { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */
655 { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */
656 { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */
657 { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */
658 { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */
659 { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */
660 { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */
661 { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */
662 { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */
663 { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */
664 };
665
666 if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
667 return -EINVAL;
668
669 info->preset = preset;
670 info->width = dv_presets[preset].width;
671 info->height = dv_presets[preset].height;
672 strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
673 return 0;
674}
675EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);
79c6ff93 676
3fd8e647
HV
677const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
678 const struct v4l2_discrete_probe *probe,
679 s32 width, s32 height)
79c6ff93
GL
680{
681 int i;
682 u32 error, min_error = UINT_MAX;
3fd8e647 683 const struct v4l2_frmsize_discrete *size, *best = NULL;
79c6ff93
GL
684
685 if (!probe)
686 return best;
687
688 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
689 error = abs(size->width - width) + abs(size->height - height);
690 if (error < min_error) {
691 min_error = error;
692 best = size;
693 }
694 if (!error)
695 break;
696 }
697
698 return best;
699}
700EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
This page took 0.667997 seconds and 5 git commands to generate.