[media] v4l: removal of old, obsolete ioctls
[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>
5e453dc7 62#include <media/v4l2-common.h>
dd99120c 63#include <media/v4l2-device.h>
0996517c 64#include <media/v4l2-ctrls.h>
3434eb7e 65#include <media/v4l2-chip-ident.h>
1da177e4 66
33b687cf 67#include <linux/videodev2.h>
1da177e4
LT
68
69MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr");
70MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers");
71MODULE_LICENSE("GPL");
72
73/*
74 *
75 * V 4 L 2 D R I V E R H E L P E R A P I
76 *
77 */
78
79/*
80 * Video Standard Operations (contributed by Michael Schimek)
81 */
82
1da177e4 83
1da177e4
LT
84/* ----------------------------------------------------------------- */
85/* priority handling */
86
87#define V4L2_PRIO_VALID(val) (val == V4L2_PRIORITY_BACKGROUND || \
88 val == V4L2_PRIORITY_INTERACTIVE || \
89 val == V4L2_PRIORITY_RECORD)
90
ffb4877b 91void v4l2_prio_init(struct v4l2_prio_state *global)
1da177e4 92{
ffb4877b 93 memset(global, 0, sizeof(*global));
1da177e4 94}
057596ee 95EXPORT_SYMBOL(v4l2_prio_init);
1da177e4
LT
96
97int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
98 enum v4l2_priority new)
99{
100 if (!V4L2_PRIO_VALID(new))
101 return -EINVAL;
102 if (*local == new)
103 return 0;
104
105 atomic_inc(&global->prios[new]);
106 if (V4L2_PRIO_VALID(*local))
107 atomic_dec(&global->prios[*local]);
108 *local = new;
109 return 0;
110}
057596ee 111EXPORT_SYMBOL(v4l2_prio_change);
1da177e4 112
ffb4877b 113void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
1da177e4 114{
ffb4877b 115 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
1da177e4 116}
057596ee 117EXPORT_SYMBOL(v4l2_prio_open);
1da177e4 118
ffb4877b 119void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
1da177e4 120{
ffb4877b
HV
121 if (V4L2_PRIO_VALID(local))
122 atomic_dec(&global->prios[local]);
1da177e4 123}
057596ee 124EXPORT_SYMBOL(v4l2_prio_close);
1da177e4
LT
125
126enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
127{
128 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
129 return V4L2_PRIORITY_RECORD;
130 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
131 return V4L2_PRIORITY_INTERACTIVE;
132 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
133 return V4L2_PRIORITY_BACKGROUND;
134 return V4L2_PRIORITY_UNSET;
135}
057596ee 136EXPORT_SYMBOL(v4l2_prio_max);
1da177e4 137
ffb4877b 138int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
1da177e4 139{
ffb4877b 140 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
1da177e4 141}
057596ee 142EXPORT_SYMBOL(v4l2_prio_check);
1da177e4
LT
143
144/* ----------------------------------------------------------------- */
145
9cb2318b
HV
146/* Helper functions for control handling */
147
148/* Check for correctness of the ctrl's value based on the data from
149 struct v4l2_queryctrl and the available menu items. Note that
150 menu_items may be NULL, in that case it is ignored. */
151int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
513521ea 152 const char * const *menu_items)
9cb2318b
HV
153{
154 if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED)
155 return -EINVAL;
156 if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED)
157 return -EBUSY;
6b5a9492
HV
158 if (qctrl->type == V4L2_CTRL_TYPE_STRING)
159 return 0;
9cb2318b
HV
160 if (qctrl->type == V4L2_CTRL_TYPE_BUTTON ||
161 qctrl->type == V4L2_CTRL_TYPE_INTEGER64 ||
162 qctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
163 return 0;
164 if (ctrl->value < qctrl->minimum || ctrl->value > qctrl->maximum)
165 return -ERANGE;
166 if (qctrl->type == V4L2_CTRL_TYPE_MENU && menu_items != NULL) {
167 if (menu_items[ctrl->value] == NULL ||
168 menu_items[ctrl->value][0] == '\0')
169 return -EINVAL;
170 }
171 return 0;
172}
057596ee 173EXPORT_SYMBOL(v4l2_ctrl_check);
9cb2318b 174
69028d70
HV
175/* Fill in a struct v4l2_queryctrl */
176int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def)
177{
0996517c
HV
178 const char *name;
179
180 v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type,
181 &min, &max, &step, &def, &qctrl->flags);
69028d70 182
69028d70
HV
183 if (name == NULL)
184 return -EINVAL;
185
9cb2318b
HV
186 qctrl->minimum = min;
187 qctrl->maximum = max;
188 qctrl->step = step;
189 qctrl->default_value = def;
190 qctrl->reserved[0] = qctrl->reserved[1] = 0;
b634a93f 191 strlcpy(qctrl->name, name, sizeof(qctrl->name));
9cb2318b
HV
192 return 0;
193}
057596ee 194EXPORT_SYMBOL(v4l2_ctrl_query_fill);
9cb2318b 195
9cb2318b 196/* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and
e281db58
HV
197 the menu. The qctrl pointer may be NULL, in which case it is ignored.
198 If menu_items is NULL, then the menu items are retrieved using
199 v4l2_ctrl_get_menu. */
9cb2318b 200int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl,
513521ea 201 const char * const *menu_items)
9cb2318b
HV
202{
203 int i;
204
1e551266 205 qmenu->reserved = 0;
e281db58
HV
206 if (menu_items == NULL)
207 menu_items = v4l2_ctrl_get_menu(qmenu->id);
9cb2318b
HV
208 if (menu_items == NULL ||
209 (qctrl && (qmenu->index < qctrl->minimum || qmenu->index > qctrl->maximum)))
210 return -EINVAL;
211 for (i = 0; i < qmenu->index && menu_items[i]; i++) ;
212 if (menu_items[i] == NULL || menu_items[i][0] == '\0')
213 return -EINVAL;
b634a93f 214 strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name));
9cb2318b
HV
215 return 0;
216}
057596ee 217EXPORT_SYMBOL(v4l2_ctrl_query_menu);
9cb2318b 218
1e551266
HV
219/* Fill in a struct v4l2_querymenu based on the specified array of valid
220 menu items (terminated by V4L2_CTRL_MENU_IDS_END).
221 Use this if there are 'holes' in the list of valid menu items. */
222int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids)
223{
513521ea 224 const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id);
1e551266
HV
225
226 qmenu->reserved = 0;
227 if (menu_items == NULL || ids == NULL)
228 return -EINVAL;
229 while (*ids != V4L2_CTRL_MENU_IDS_END) {
230 if (*ids++ == qmenu->index) {
b634a93f
HV
231 strlcpy(qmenu->name, menu_items[qmenu->index],
232 sizeof(qmenu->name));
1e551266
HV
233 return 0;
234 }
235 }
236 return -EINVAL;
237}
238EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items);
239
9cb2318b
HV
240/* ctrl_classes points to an array of u32 pointers, the last element is
241 a NULL pointer. Each u32 array is a 0-terminated array of control IDs.
242 Each array must be sorted low to high and belong to the same control
2ba58894 243 class. The array of u32 pointers must also be sorted, from low class IDs
9cb2318b
HV
244 to high class IDs.
245
246 This function returns the first ID that follows after the given ID.
247 When no more controls are available 0 is returned. */
248u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id)
249{
a46c5fbc 250 u32 ctrl_class = V4L2_CTRL_ID2CLASS(id);
9cb2318b
HV
251 const u32 *pctrl;
252
9cb2318b
HV
253 if (ctrl_classes == NULL)
254 return 0;
a46c5fbc
HV
255
256 /* if no query is desired, then check if the ID is part of ctrl_classes */
257 if ((id & V4L2_CTRL_FLAG_NEXT_CTRL) == 0) {
258 /* find class */
259 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) != ctrl_class)
260 ctrl_classes++;
261 if (*ctrl_classes == NULL)
262 return 0;
263 pctrl = *ctrl_classes;
264 /* find control ID */
265 while (*pctrl && *pctrl != id) pctrl++;
266 return *pctrl ? id : 0;
267 }
9cb2318b 268 id &= V4L2_CTRL_ID_MASK;
9cb2318b
HV
269 id++; /* select next control */
270 /* find first class that matches (or is greater than) the class of
271 the ID */
272 while (*ctrl_classes && V4L2_CTRL_ID2CLASS(**ctrl_classes) < ctrl_class)
273 ctrl_classes++;
274 /* no more classes */
275 if (*ctrl_classes == NULL)
276 return 0;
277 pctrl = *ctrl_classes;
278 /* find first ctrl within the class that is >= ID */
279 while (*pctrl && *pctrl < id) pctrl++;
280 if (*pctrl)
281 return *pctrl;
282 /* we are at the end of the controls of the current class. */
283 /* continue with next class if available */
284 ctrl_classes++;
285 if (*ctrl_classes == NULL)
286 return 0;
287 return **ctrl_classes;
288}
057596ee 289EXPORT_SYMBOL(v4l2_ctrl_next);
9cb2318b 290
aecde8b5 291int v4l2_chip_match_host(const struct v4l2_dbg_match *match)
a9254475 292{
aecde8b5 293 switch (match->type) {
a9254475 294 case V4L2_CHIP_MATCH_HOST:
aecde8b5 295 return match->addr == 0;
a9254475
MCC
296 default:
297 return 0;
298 }
299}
300EXPORT_SYMBOL(v4l2_chip_match_host);
301
302#if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
aecde8b5 303int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match)
f3d092b8 304{
aecde8b5
HV
305 int len;
306
307 if (c == NULL || match == NULL)
308 return 0;
309
310 switch (match->type) {
f3d092b8 311 case V4L2_CHIP_MATCH_I2C_DRIVER:
aecde8b5
HV
312 if (c->driver == NULL || c->driver->driver.name == NULL)
313 return 0;
314 len = strlen(c->driver->driver.name);
315 /* legacy drivers have a ' suffix, don't try to match that */
316 if (len && c->driver->driver.name[len - 1] == '\'')
317 len--;
318 return len && !strncmp(c->driver->driver.name, match->name, len);
f3d092b8 319 case V4L2_CHIP_MATCH_I2C_ADDR:
aecde8b5 320 return c->addr == match->addr;
f3d092b8
HV
321 default:
322 return 0;
323 }
324}
a9254475 325EXPORT_SYMBOL(v4l2_chip_match_i2c_client);
f3d092b8 326
aecde8b5 327int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip,
3434eb7e
HV
328 u32 ident, u32 revision)
329{
aecde8b5 330 if (!v4l2_chip_match_i2c_client(c, &chip->match))
3434eb7e
HV
331 return 0;
332 if (chip->ident == V4L2_IDENT_NONE) {
333 chip->ident = ident;
334 chip->revision = revision;
335 }
336 else {
337 chip->ident = V4L2_IDENT_AMBIGUOUS;
338 chip->revision = 0;
339 }
340 return 0;
341}
a9254475 342EXPORT_SYMBOL(v4l2_chip_ident_i2c_client);
f3d092b8 343
9cb2318b
HV
344/* ----------------------------------------------------------------- */
345
78a3b4db 346/* I2C Helper functions */
8ffbc655 347
dd99120c
HV
348
349void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
350 const struct v4l2_subdev_ops *ops)
351{
352 v4l2_subdev_init(sd, ops);
b5b2b7ed 353 sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
dd99120c
HV
354 /* the owner is the same as the i2c_client's driver owner */
355 sd->owner = client->driver->driver.owner;
356 /* i2c_client and v4l2_subdev point to one another */
357 v4l2_set_subdevdata(sd, client);
358 i2c_set_clientdata(client, sd);
359 /* initialize name */
360 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
361 client->driver->driver.name, i2c_adapter_id(client->adapter),
362 client->addr);
363}
364EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
365
366
367
f0222c7d
HV
368/* Load an i2c sub-device. */
369struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev,
9a1f8b34
LP
370 struct i2c_adapter *adapter, struct i2c_board_info *info,
371 const unsigned short *probe_addrs)
f0222c7d
HV
372{
373 struct v4l2_subdev *sd = NULL;
374 struct i2c_client *client;
375
376 BUG_ON(!v4l2_dev);
377
9a1f8b34 378 request_module(I2C_MODULE_PREFIX "%s", info->type);
f0222c7d
HV
379
380 /* Create the i2c client */
381 if (info->addr == 0 && probe_addrs)
9a94241a
JD
382 client = i2c_new_probed_device(adapter, info, probe_addrs,
383 NULL);
f0222c7d
HV
384 else
385 client = i2c_new_device(adapter, info);
386
387 /* Note: by loading the module first we are certain that c->driver
388 will be set if the driver was found. If the module was not loaded
389 first, then the i2c core tries to delay-load the module for us,
390 and then c->driver is still NULL until the module is finally
391 loaded. This delay-load mechanism doesn't work if other drivers
392 want to use the i2c device, so explicitly loading the module
393 is the best alternative. */
394 if (client == NULL || client->driver == NULL)
395 goto error;
396
397 /* Lock the module so we can safely get the v4l2_subdev pointer */
398 if (!try_module_get(client->driver->driver.owner))
399 goto error;
400 sd = i2c_get_clientdata(client);
401
402 /* Register with the v4l2_device which increases the module's
403 use count as well. */
404 if (v4l2_device_register_subdev(v4l2_dev, sd))
405 sd = NULL;
406 /* Decrease the module use count to match the first try_module_get. */
407 module_put(client->driver->driver.owner);
408
f0222c7d
HV
409error:
410 /* If we have a client but no subdev, then something went wrong and
411 we must unregister the client. */
412 if (client && sd == NULL)
413 i2c_unregister_device(client);
414 return sd;
415}
416EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board);
417
3c7c9370 418struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev,
9a1f8b34 419 struct i2c_adapter *adapter, const char *client_type,
f0222c7d
HV
420 u8 addr, const unsigned short *probe_addrs)
421{
422 struct i2c_board_info info;
423
424 /* Setup the i2c board info with the device type and
425 the device address. */
426 memset(&info, 0, sizeof(info));
427 strlcpy(info.type, client_type, sizeof(info.type));
428 info.addr = addr;
f0222c7d 429
9a1f8b34 430 return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs);
f0222c7d 431}
3c7c9370 432EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev);
f0222c7d 433
ab373190
HV
434/* Return i2c client address of v4l2_subdev. */
435unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd)
436{
437 struct i2c_client *client = v4l2_get_subdevdata(sd);
438
439 return client ? client->addr : I2C_CLIENT_END;
440}
441EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr);
442
c7d29e2f
HV
443/* Return a list of I2C tuner addresses to probe. Use only if the tuner
444 addresses are unknown. */
445const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type)
446{
447 static const unsigned short radio_addrs[] = {
448#if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE)
449 0x10,
450#endif
451 0x60,
452 I2C_CLIENT_END
453 };
454 static const unsigned short demod_addrs[] = {
455 0x42, 0x43, 0x4a, 0x4b,
456 I2C_CLIENT_END
457 };
458 static const unsigned short tv_addrs[] = {
459 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
416a7aa8 460 0x60, 0x61, 0x62, 0x63, 0x64,
c7d29e2f
HV
461 I2C_CLIENT_END
462 };
463
464 switch (type) {
465 case ADDRS_RADIO:
466 return radio_addrs;
467 case ADDRS_DEMOD:
468 return demod_addrs;
469 case ADDRS_TV:
470 return tv_addrs;
471 case ADDRS_TV_WITH_DEMOD:
472 return tv_addrs + 4;
473 }
474 return NULL;
475}
476EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs);
477
d8b29966
TP
478#endif /* defined(CONFIG_I2C) */
479
85e09219
DB
480#if defined(CONFIG_SPI)
481
482/* Load a spi sub-device. */
483
484void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi,
485 const struct v4l2_subdev_ops *ops)
486{
487 v4l2_subdev_init(sd, ops);
488 sd->flags |= V4L2_SUBDEV_FL_IS_SPI;
489 /* the owner is the same as the spi_device's driver owner */
490 sd->owner = spi->dev.driver->owner;
491 /* spi_device and v4l2_subdev point to one another */
492 v4l2_set_subdevdata(sd, spi);
493 spi_set_drvdata(spi, sd);
494 /* initialize name */
495 strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name));
496}
497EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init);
498
499struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
500 struct spi_master *master, struct spi_board_info *info)
501{
502 struct v4l2_subdev *sd = NULL;
503 struct spi_device *spi = NULL;
504
505 BUG_ON(!v4l2_dev);
506
507 if (info->modalias)
508 request_module(info->modalias);
509
510 spi = spi_new_device(master, info);
511
512 if (spi == NULL || spi->dev.driver == NULL)
513 goto error;
514
515 if (!try_module_get(spi->dev.driver->owner))
516 goto error;
517
518 sd = spi_get_drvdata(spi);
519
520 /* Register with the v4l2_device which increases the module's
521 use count as well. */
522 if (v4l2_device_register_subdev(v4l2_dev, sd))
523 sd = NULL;
524
525 /* Decrease the module use count to match the first try_module_get. */
526 module_put(spi->dev.driver->owner);
527
528error:
529 /* If we have a client but no subdev, then something went wrong and
530 we must unregister the client. */
531 if (spi && sd == NULL)
532 spi_unregister_device(spi);
533
534 return sd;
535}
536EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev);
537
538#endif /* defined(CONFIG_SPI) */
539
b0d3159b
TP
540/* Clamp x to be between min and max, aligned to a multiple of 2^align. min
541 * and max don't have to be aligned, but there must be at least one valid
542 * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples
543 * of 16 between 17 and 31. */
544static unsigned int clamp_align(unsigned int x, unsigned int min,
545 unsigned int max, unsigned int align)
546{
547 /* Bits that must be zero to be aligned */
548 unsigned int mask = ~((1 << align) - 1);
549
550 /* Round to nearest aligned value */
551 if (align)
552 x = (x + (1 << (align - 1))) & mask;
553
554 /* Clamp to aligned value of min and max */
555 if (x < min)
556 x = (min + ~mask) & mask;
557 else if (x > max)
558 x = max & mask;
559
560 return x;
561}
562
563/* Bound an image to have a width between wmin and wmax, and height between
564 * hmin and hmax, inclusive. Additionally, the width will be a multiple of
565 * 2^walign, the height will be a multiple of 2^halign, and the overall size
566 * (width*height) will be a multiple of 2^salign. The image may be shrunk
567 * or enlarged to fit the alignment constraints.
568 *
569 * The width or height maximum must not be smaller than the corresponding
570 * minimum. The alignments must not be so high there are no possible image
571 * sizes within the allowed bounds. wmin and hmin must be at least 1
572 * (don't use 0). If you don't care about a certain alignment, specify 0,
573 * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If
574 * you only want to adjust downward, specify a maximum that's the same as
575 * the initial value.
576 */
577void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
578 unsigned int walign,
579 u32 *h, unsigned int hmin, unsigned int hmax,
580 unsigned int halign, unsigned int salign)
581{
582 *w = clamp_align(*w, wmin, wmax, walign);
583 *h = clamp_align(*h, hmin, hmax, halign);
584
585 /* Usually we don't need to align the size and are done now. */
586 if (!salign)
587 return;
588
589 /* How much alignment do we have? */
590 walign = __ffs(*w);
591 halign = __ffs(*h);
592 /* Enough to satisfy the image alignment? */
593 if (walign + halign < salign) {
594 /* Max walign where there is still a valid width */
595 unsigned int wmaxa = __fls(wmax ^ (wmin - 1));
596 /* Max halign where there is still a valid height */
597 unsigned int hmaxa = __fls(hmax ^ (hmin - 1));
598
599 /* up the smaller alignment until we have enough */
600 do {
601 if (halign >= hmaxa ||
602 (walign <= halign && walign < wmaxa)) {
603 *w = clamp_align(*w, wmin, wmax, walign + 1);
604 walign = __ffs(*w);
605 } else {
606 *h = clamp_align(*h, hmin, hmax, halign + 1);
607 halign = __ffs(*h);
608 }
609 } while (halign + walign < salign);
610 }
611}
612EXPORT_SYMBOL_GPL(v4l_bound_align_image);
2e535ed5
MK
613
614/**
615 * v4l_fill_dv_preset_info - fill description of a digital video preset
616 * @preset - preset value
617 * @info - pointer to struct v4l2_dv_enum_preset
618 *
619 * drivers can use this helper function to fill description of dv preset
620 * in info.
621 */
622int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info)
623{
624 static const struct v4l2_dv_preset_info {
625 u16 width;
626 u16 height;
627 const char *name;
628 } dv_presets[] = {
629 { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */
630 { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */
631 { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */
632 { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */
633 { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */
634 { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */
635 { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */
636 { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */
637 { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */
638 { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */
639 { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */
640 { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */
641 { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */
642 { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */
643 { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */
644 { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */
645 { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */
646 { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */
647 { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */
648 };
649
650 if (info == NULL || preset >= ARRAY_SIZE(dv_presets))
651 return -EINVAL;
652
653 info->preset = preset;
654 info->width = dv_presets[preset].width;
655 info->height = dv_presets[preset].height;
656 strlcpy(info->name, dv_presets[preset].name, sizeof(info->name));
657 return 0;
658}
659EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info);
79c6ff93 660
3fd8e647
HV
661const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
662 const struct v4l2_discrete_probe *probe,
663 s32 width, s32 height)
79c6ff93
GL
664{
665 int i;
666 u32 error, min_error = UINT_MAX;
3fd8e647 667 const struct v4l2_frmsize_discrete *size, *best = NULL;
79c6ff93
GL
668
669 if (!probe)
670 return best;
671
672 for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) {
673 error = abs(size->width - width) + abs(size->height - height);
674 if (error < min_error) {
675 min_error = error;
676 best = size;
677 }
678 if (!error)
679 break;
680 }
681
682 return best;
683}
684EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
This page took 0.867644 seconds and 5 git commands to generate.