Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Mauro Carvalho Chehab | 43db48d | 2007-01-09 11:20:59 -0300 | [diff] [blame] | 15 | * Author: Bill Dirks <bill@thedirks.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Alan Cox | d9b0144 | 2008-10-27 15:13:47 -0300 | [diff] [blame] | 31 | * Author: Alan Cox, <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 40 | * Iñaki García Etxebarria <garetxe@euskalnet.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/module.h> |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
| 51 | #include <linux/string.h> |
| 52 | #include <linux/errno.h> |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 53 | #include <linux/i2c.h> |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 54 | #if defined(CONFIG_SPI) |
| 55 | #include <linux/spi/spi.h> |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 401998f | 2006-06-04 10:06:18 -0300 | [diff] [blame] | 62 | #define __OLD_VIDIOC_ /* To allow fixing old calls*/ |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 63 | #include <media/v4l2-common.h> |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 64 | #include <media/v4l2-device.h> |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 65 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 66 | #include <media/v4l2-chip-ident.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Hans Verkuil | 33b687c | 2008-07-25 05:32:50 -0300 | [diff] [blame] | 68 | #include <linux/videodev2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | MODULE_AUTHOR("Bill Dirks, Justin Schoeman, Gerd Knorr"); |
| 71 | MODULE_DESCRIPTION("misc helper functions for v4l2 device drivers"); |
| 72 | MODULE_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 92 | void v4l2_prio_init(struct v4l2_prio_state *global) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 94 | memset(global, 0, sizeof(*global)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 96 | EXPORT_SYMBOL(v4l2_prio_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | int 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 Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 112 | EXPORT_SYMBOL(v4l2_prio_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 114 | void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 116 | v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 118 | EXPORT_SYMBOL(v4l2_prio_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 120 | void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 122 | if (V4L2_PRIO_VALID(local)) |
| 123 | atomic_dec(&global->prios[local]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 125 | EXPORT_SYMBOL(v4l2_prio_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | enum 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 Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 137 | EXPORT_SYMBOL(v4l2_prio_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 139 | int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Hans Verkuil | ffb4877 | 2010-05-01 08:03:24 -0300 | [diff] [blame] | 141 | return (local < v4l2_prio_max(global)) ? -EBUSY : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 143 | EXPORT_SYMBOL(v4l2_prio_check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* ----------------------------------------------------------------- */ |
| 146 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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. */ |
| 152 | int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl, |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 153 | const char * const *menu_items) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 154 | { |
| 155 | if (qctrl->flags & V4L2_CTRL_FLAG_DISABLED) |
| 156 | return -EINVAL; |
| 157 | if (qctrl->flags & V4L2_CTRL_FLAG_GRABBED) |
| 158 | return -EBUSY; |
Hans Verkuil | 6b5a949 | 2009-08-11 18:47:18 -0300 | [diff] [blame] | 159 | if (qctrl->type == V4L2_CTRL_TYPE_STRING) |
| 160 | return 0; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 174 | EXPORT_SYMBOL(v4l2_ctrl_check); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 175 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 176 | /* Fill in a struct v4l2_queryctrl */ |
| 177 | int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 min, s32 max, s32 step, s32 def) |
| 178 | { |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 179 | const char *name; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 180 | |
Hans Verkuil | 0996517 | 2010-08-01 14:32:42 -0300 | [diff] [blame] | 181 | v4l2_ctrl_fill(qctrl->id, &name, &qctrl->type, |
| 182 | &min, &max, &step, &def, &qctrl->flags); |
| 183 | |
Hans Verkuil | 69028d7 | 2008-08-08 07:55:00 -0300 | [diff] [blame] | 184 | if (name == NULL) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 185 | return -EINVAL; |
Hans Verkuil | 69028d7 | 2008-08-08 07:55:00 -0300 | [diff] [blame] | 186 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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; |
Hans Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 192 | strlcpy(qctrl->name, name, sizeof(qctrl->name)); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 193 | return 0; |
| 194 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 195 | EXPORT_SYMBOL(v4l2_ctrl_query_fill); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 196 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 197 | /* Fill in a struct v4l2_querymenu based on the struct v4l2_queryctrl and |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 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. */ |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 201 | int v4l2_ctrl_query_menu(struct v4l2_querymenu *qmenu, struct v4l2_queryctrl *qctrl, |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 202 | const char * const *menu_items) |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 203 | { |
| 204 | int i; |
| 205 | |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 206 | qmenu->reserved = 0; |
Hans Verkuil | e281db58 | 2008-08-08 12:43:59 -0300 | [diff] [blame] | 207 | if (menu_items == NULL) |
| 208 | menu_items = v4l2_ctrl_get_menu(qmenu->id); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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; |
Hans Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 215 | strlcpy(qmenu->name, menu_items[qmenu->index], sizeof(qmenu->name)); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 216 | return 0; |
| 217 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 218 | EXPORT_SYMBOL(v4l2_ctrl_query_menu); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 219 | |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 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. */ |
| 223 | int v4l2_ctrl_query_menu_valid_items(struct v4l2_querymenu *qmenu, const u32 *ids) |
| 224 | { |
Hans Verkuil | 513521e | 2010-12-29 14:25:52 -0300 | [diff] [blame] | 225 | const char * const *menu_items = v4l2_ctrl_get_menu(qmenu->id); |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 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 Verkuil | b634a93 | 2009-01-17 11:26:59 -0300 | [diff] [blame] | 232 | strlcpy(qmenu->name, menu_items[qmenu->index], |
| 233 | sizeof(qmenu->name)); |
Hans Verkuil | 1e55126 | 2008-08-08 08:34:19 -0300 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | EXPORT_SYMBOL(v4l2_ctrl_query_menu_valid_items); |
| 240 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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 |
Hans Verkuil | 2ba5889 | 2009-02-13 10:57:48 -0300 | [diff] [blame] | 244 | class. The array of u32 pointers must also be sorted, from low class IDs |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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. */ |
| 249 | u32 v4l2_ctrl_next(const u32 * const * ctrl_classes, u32 id) |
| 250 | { |
Hans Verkuil | a46c5fb | 2007-07-19 04:53:36 -0300 | [diff] [blame] | 251 | u32 ctrl_class = V4L2_CTRL_ID2CLASS(id); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 252 | const u32 *pctrl; |
| 253 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 254 | if (ctrl_classes == NULL) |
| 255 | return 0; |
Hans Verkuil | a46c5fb | 2007-07-19 04:53:36 -0300 | [diff] [blame] | 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 | } |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 269 | id &= V4L2_CTRL_ID_MASK; |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 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 | } |
Mauro Carvalho Chehab | 057596e | 2008-02-02 11:25:31 -0300 | [diff] [blame] | 290 | EXPORT_SYMBOL(v4l2_ctrl_next); |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 291 | |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 292 | int v4l2_chip_match_host(const struct v4l2_dbg_match *match) |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 293 | { |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 294 | switch (match->type) { |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 295 | case V4L2_CHIP_MATCH_HOST: |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 296 | return match->addr == 0; |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 297 | default: |
| 298 | return 0; |
| 299 | } |
| 300 | } |
| 301 | EXPORT_SYMBOL(v4l2_chip_match_host); |
| 302 | |
| 303 | #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE)) |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 304 | int v4l2_chip_match_i2c_client(struct i2c_client *c, const struct v4l2_dbg_match *match) |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 305 | { |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 306 | int len; |
| 307 | |
| 308 | if (c == NULL || match == NULL) |
| 309 | return 0; |
| 310 | |
| 311 | switch (match->type) { |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 312 | case V4L2_CHIP_MATCH_I2C_DRIVER: |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 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); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 320 | case V4L2_CHIP_MATCH_I2C_ADDR: |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 321 | return c->addr == match->addr; |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 322 | default: |
| 323 | return 0; |
| 324 | } |
| 325 | } |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 326 | EXPORT_SYMBOL(v4l2_chip_match_i2c_client); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 327 | |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 328 | int v4l2_chip_ident_i2c_client(struct i2c_client *c, struct v4l2_dbg_chip_ident *chip, |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 329 | u32 ident, u32 revision) |
| 330 | { |
Hans Verkuil | aecde8b | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 331 | if (!v4l2_chip_match_i2c_client(c, &chip->match)) |
Hans Verkuil | 3434eb7 | 2007-04-27 12:31:08 -0300 | [diff] [blame] | 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 | } |
Mauro Carvalho Chehab | a925447 | 2008-01-29 18:32:35 -0300 | [diff] [blame] | 343 | EXPORT_SYMBOL(v4l2_chip_ident_i2c_client); |
Hans Verkuil | f3d092b | 2007-02-23 20:55:14 -0300 | [diff] [blame] | 344 | |
Hans Verkuil | 9cb2318 | 2006-06-18 14:11:08 -0300 | [diff] [blame] | 345 | /* ----------------------------------------------------------------- */ |
| 346 | |
Hans Verkuil | 78a3b4d | 2009-04-01 03:41:09 -0300 | [diff] [blame] | 347 | /* I2C Helper functions */ |
Hans Verkuil | 8ffbc65 | 2007-09-12 08:32:50 -0300 | [diff] [blame] | 348 | |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 349 | |
| 350 | void 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 Verkuil | b5b2b7e | 2009-05-02 10:58:51 -0300 | [diff] [blame] | 354 | sd->flags |= V4L2_SUBDEV_FL_IS_I2C; |
Hans Verkuil | dd99120 | 2008-11-23 12:19:45 -0300 | [diff] [blame] | 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 | } |
| 365 | EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); |
| 366 | |
| 367 | |
| 368 | |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 369 | /* Load an i2c sub-device. */ |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 370 | struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 371 | struct i2c_adapter *adapter, struct i2c_board_info *info, |
| 372 | const unsigned short *probe_addrs) |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 373 | { |
| 374 | struct v4l2_subdev *sd = NULL; |
| 375 | struct i2c_client *client; |
| 376 | |
| 377 | BUG_ON(!v4l2_dev); |
| 378 | |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 379 | request_module(I2C_MODULE_PREFIX "%s", info->type); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 380 | |
| 381 | /* Create the i2c client */ |
| 382 | if (info->addr == 0 && probe_addrs) |
Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 383 | client = i2c_new_probed_device(adapter, info, probe_addrs, |
| 384 | NULL); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 410 | error: |
| 411 | /* If we have a client but no subdev, then something went wrong and |
| 412 | we must unregister the client. */ |
| 413 | if (client && sd == NULL) |
| 414 | i2c_unregister_device(client); |
| 415 | return sd; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev_board); |
| 418 | |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame^] | 419 | struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 420 | struct i2c_adapter *adapter, const char *client_type, |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 421 | u8 addr, const unsigned short *probe_addrs) |
| 422 | { |
| 423 | struct i2c_board_info info; |
| 424 | |
| 425 | /* Setup the i2c board info with the device type and |
| 426 | the device address. */ |
| 427 | memset(&info, 0, sizeof(info)); |
| 428 | strlcpy(info.type, client_type, sizeof(info.type)); |
| 429 | info.addr = addr; |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 430 | |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 431 | return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 432 | } |
Hans Verkuil | 3c7c937 | 2011-01-08 07:08:02 -0300 | [diff] [blame^] | 433 | EXPORT_SYMBOL_GPL(v4l2_i2c_new_subdev); |
Hans Verkuil | f0222c7 | 2009-06-09 17:12:33 -0300 | [diff] [blame] | 434 | |
Hans Verkuil | ab37319 | 2009-02-21 18:08:41 -0300 | [diff] [blame] | 435 | /* Return i2c client address of v4l2_subdev. */ |
| 436 | unsigned short v4l2_i2c_subdev_addr(struct v4l2_subdev *sd) |
| 437 | { |
| 438 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 439 | |
| 440 | return client ? client->addr : I2C_CLIENT_END; |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_addr); |
| 443 | |
Hans Verkuil | c7d29e2 | 2009-01-18 19:37:59 -0300 | [diff] [blame] | 444 | /* Return a list of I2C tuner addresses to probe. Use only if the tuner |
| 445 | addresses are unknown. */ |
| 446 | const unsigned short *v4l2_i2c_tuner_addrs(enum v4l2_i2c_tuner_type type) |
| 447 | { |
| 448 | static const unsigned short radio_addrs[] = { |
| 449 | #if defined(CONFIG_MEDIA_TUNER_TEA5761) || defined(CONFIG_MEDIA_TUNER_TEA5761_MODULE) |
| 450 | 0x10, |
| 451 | #endif |
| 452 | 0x60, |
| 453 | I2C_CLIENT_END |
| 454 | }; |
| 455 | static const unsigned short demod_addrs[] = { |
| 456 | 0x42, 0x43, 0x4a, 0x4b, |
| 457 | I2C_CLIENT_END |
| 458 | }; |
| 459 | static const unsigned short tv_addrs[] = { |
| 460 | 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */ |
Hans Verkuil | 416a7aa | 2009-05-02 09:42:57 -0300 | [diff] [blame] | 461 | 0x60, 0x61, 0x62, 0x63, 0x64, |
Hans Verkuil | c7d29e2 | 2009-01-18 19:37:59 -0300 | [diff] [blame] | 462 | I2C_CLIENT_END |
| 463 | }; |
| 464 | |
| 465 | switch (type) { |
| 466 | case ADDRS_RADIO: |
| 467 | return radio_addrs; |
| 468 | case ADDRS_DEMOD: |
| 469 | return demod_addrs; |
| 470 | case ADDRS_TV: |
| 471 | return tv_addrs; |
| 472 | case ADDRS_TV_WITH_DEMOD: |
| 473 | return tv_addrs + 4; |
| 474 | } |
| 475 | return NULL; |
| 476 | } |
| 477 | EXPORT_SYMBOL_GPL(v4l2_i2c_tuner_addrs); |
| 478 | |
Trent Piepho | d8b2996 | 2009-06-12 16:31:29 -0300 | [diff] [blame] | 479 | #endif /* defined(CONFIG_I2C) */ |
| 480 | |
Dmitri Belimov | 85e0921 | 2010-03-23 11:23:29 -0300 | [diff] [blame] | 481 | #if defined(CONFIG_SPI) |
| 482 | |
| 483 | /* Load a spi sub-device. */ |
| 484 | |
| 485 | void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi, |
| 486 | const struct v4l2_subdev_ops *ops) |
| 487 | { |
| 488 | v4l2_subdev_init(sd, ops); |
| 489 | sd->flags |= V4L2_SUBDEV_FL_IS_SPI; |
| 490 | /* the owner is the same as the spi_device's driver owner */ |
| 491 | sd->owner = spi->dev.driver->owner; |
| 492 | /* spi_device and v4l2_subdev point to one another */ |
| 493 | v4l2_set_subdevdata(sd, spi); |
| 494 | spi_set_drvdata(spi, sd); |
| 495 | /* initialize name */ |
| 496 | strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name)); |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init); |
| 499 | |
| 500 | struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev, |
| 501 | struct spi_master *master, struct spi_board_info *info) |
| 502 | { |
| 503 | struct v4l2_subdev *sd = NULL; |
| 504 | struct spi_device *spi = NULL; |
| 505 | |
| 506 | BUG_ON(!v4l2_dev); |
| 507 | |
| 508 | if (info->modalias) |
| 509 | request_module(info->modalias); |
| 510 | |
| 511 | spi = spi_new_device(master, info); |
| 512 | |
| 513 | if (spi == NULL || spi->dev.driver == NULL) |
| 514 | goto error; |
| 515 | |
| 516 | if (!try_module_get(spi->dev.driver->owner)) |
| 517 | goto error; |
| 518 | |
| 519 | sd = spi_get_drvdata(spi); |
| 520 | |
| 521 | /* Register with the v4l2_device which increases the module's |
| 522 | use count as well. */ |
| 523 | if (v4l2_device_register_subdev(v4l2_dev, sd)) |
| 524 | sd = NULL; |
| 525 | |
| 526 | /* Decrease the module use count to match the first try_module_get. */ |
| 527 | module_put(spi->dev.driver->owner); |
| 528 | |
| 529 | error: |
| 530 | /* If we have a client but no subdev, then something went wrong and |
| 531 | we must unregister the client. */ |
| 532 | if (spi && sd == NULL) |
| 533 | spi_unregister_device(spi); |
| 534 | |
| 535 | return sd; |
| 536 | } |
| 537 | EXPORT_SYMBOL_GPL(v4l2_spi_new_subdev); |
| 538 | |
| 539 | #endif /* defined(CONFIG_SPI) */ |
| 540 | |
Trent Piepho | b0d3159 | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 541 | /* Clamp x to be between min and max, aligned to a multiple of 2^align. min |
| 542 | * and max don't have to be aligned, but there must be at least one valid |
| 543 | * value. E.g., min=17,max=31,align=4 is not allowed as there are no multiples |
| 544 | * of 16 between 17 and 31. */ |
| 545 | static unsigned int clamp_align(unsigned int x, unsigned int min, |
| 546 | unsigned int max, unsigned int align) |
| 547 | { |
| 548 | /* Bits that must be zero to be aligned */ |
| 549 | unsigned int mask = ~((1 << align) - 1); |
| 550 | |
| 551 | /* Round to nearest aligned value */ |
| 552 | if (align) |
| 553 | x = (x + (1 << (align - 1))) & mask; |
| 554 | |
| 555 | /* Clamp to aligned value of min and max */ |
| 556 | if (x < min) |
| 557 | x = (min + ~mask) & mask; |
| 558 | else if (x > max) |
| 559 | x = max & mask; |
| 560 | |
| 561 | return x; |
| 562 | } |
| 563 | |
| 564 | /* Bound an image to have a width between wmin and wmax, and height between |
| 565 | * hmin and hmax, inclusive. Additionally, the width will be a multiple of |
| 566 | * 2^walign, the height will be a multiple of 2^halign, and the overall size |
| 567 | * (width*height) will be a multiple of 2^salign. The image may be shrunk |
| 568 | * or enlarged to fit the alignment constraints. |
| 569 | * |
| 570 | * The width or height maximum must not be smaller than the corresponding |
| 571 | * minimum. The alignments must not be so high there are no possible image |
| 572 | * sizes within the allowed bounds. wmin and hmin must be at least 1 |
| 573 | * (don't use 0). If you don't care about a certain alignment, specify 0, |
| 574 | * as 2^0 is 1 and one byte alignment is equivalent to no alignment. If |
| 575 | * you only want to adjust downward, specify a maximum that's the same as |
| 576 | * the initial value. |
| 577 | */ |
| 578 | void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax, |
| 579 | unsigned int walign, |
| 580 | u32 *h, unsigned int hmin, unsigned int hmax, |
| 581 | unsigned int halign, unsigned int salign) |
| 582 | { |
| 583 | *w = clamp_align(*w, wmin, wmax, walign); |
| 584 | *h = clamp_align(*h, hmin, hmax, halign); |
| 585 | |
| 586 | /* Usually we don't need to align the size and are done now. */ |
| 587 | if (!salign) |
| 588 | return; |
| 589 | |
| 590 | /* How much alignment do we have? */ |
| 591 | walign = __ffs(*w); |
| 592 | halign = __ffs(*h); |
| 593 | /* Enough to satisfy the image alignment? */ |
| 594 | if (walign + halign < salign) { |
| 595 | /* Max walign where there is still a valid width */ |
| 596 | unsigned int wmaxa = __fls(wmax ^ (wmin - 1)); |
| 597 | /* Max halign where there is still a valid height */ |
| 598 | unsigned int hmaxa = __fls(hmax ^ (hmin - 1)); |
| 599 | |
| 600 | /* up the smaller alignment until we have enough */ |
| 601 | do { |
| 602 | if (halign >= hmaxa || |
| 603 | (walign <= halign && walign < wmaxa)) { |
| 604 | *w = clamp_align(*w, wmin, wmax, walign + 1); |
| 605 | walign = __ffs(*w); |
| 606 | } else { |
| 607 | *h = clamp_align(*h, hmin, hmax, halign + 1); |
| 608 | halign = __ffs(*h); |
| 609 | } |
| 610 | } while (halign + walign < salign); |
| 611 | } |
| 612 | } |
| 613 | EXPORT_SYMBOL_GPL(v4l_bound_align_image); |
Muralidharan Karicheri | 2e535ed | 2009-12-10 04:39:47 -0300 | [diff] [blame] | 614 | |
| 615 | /** |
| 616 | * v4l_fill_dv_preset_info - fill description of a digital video preset |
| 617 | * @preset - preset value |
| 618 | * @info - pointer to struct v4l2_dv_enum_preset |
| 619 | * |
| 620 | * drivers can use this helper function to fill description of dv preset |
| 621 | * in info. |
| 622 | */ |
| 623 | int v4l_fill_dv_preset_info(u32 preset, struct v4l2_dv_enum_preset *info) |
| 624 | { |
| 625 | static const struct v4l2_dv_preset_info { |
| 626 | u16 width; |
| 627 | u16 height; |
| 628 | const char *name; |
| 629 | } dv_presets[] = { |
| 630 | { 0, 0, "Invalid" }, /* V4L2_DV_INVALID */ |
| 631 | { 720, 480, "480p@59.94" }, /* V4L2_DV_480P59_94 */ |
| 632 | { 720, 576, "576p@50" }, /* V4L2_DV_576P50 */ |
| 633 | { 1280, 720, "720p@24" }, /* V4L2_DV_720P24 */ |
| 634 | { 1280, 720, "720p@25" }, /* V4L2_DV_720P25 */ |
| 635 | { 1280, 720, "720p@30" }, /* V4L2_DV_720P30 */ |
| 636 | { 1280, 720, "720p@50" }, /* V4L2_DV_720P50 */ |
| 637 | { 1280, 720, "720p@59.94" }, /* V4L2_DV_720P59_94 */ |
| 638 | { 1280, 720, "720p@60" }, /* V4L2_DV_720P60 */ |
| 639 | { 1920, 1080, "1080i@29.97" }, /* V4L2_DV_1080I29_97 */ |
| 640 | { 1920, 1080, "1080i@30" }, /* V4L2_DV_1080I30 */ |
| 641 | { 1920, 1080, "1080i@25" }, /* V4L2_DV_1080I25 */ |
| 642 | { 1920, 1080, "1080i@50" }, /* V4L2_DV_1080I50 */ |
| 643 | { 1920, 1080, "1080i@60" }, /* V4L2_DV_1080I60 */ |
| 644 | { 1920, 1080, "1080p@24" }, /* V4L2_DV_1080P24 */ |
| 645 | { 1920, 1080, "1080p@25" }, /* V4L2_DV_1080P25 */ |
| 646 | { 1920, 1080, "1080p@30" }, /* V4L2_DV_1080P30 */ |
| 647 | { 1920, 1080, "1080p@50" }, /* V4L2_DV_1080P50 */ |
| 648 | { 1920, 1080, "1080p@60" }, /* V4L2_DV_1080P60 */ |
| 649 | }; |
| 650 | |
| 651 | if (info == NULL || preset >= ARRAY_SIZE(dv_presets)) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | info->preset = preset; |
| 655 | info->width = dv_presets[preset].width; |
| 656 | info->height = dv_presets[preset].height; |
| 657 | strlcpy(info->name, dv_presets[preset].name, sizeof(info->name)); |
| 658 | return 0; |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(v4l_fill_dv_preset_info); |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 661 | |
Hans Verkuil | 3fd8e64 | 2010-09-30 09:29:37 -0300 | [diff] [blame] | 662 | const struct v4l2_frmsize_discrete *v4l2_find_nearest_format( |
| 663 | const struct v4l2_discrete_probe *probe, |
| 664 | s32 width, s32 height) |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 665 | { |
| 666 | int i; |
| 667 | u32 error, min_error = UINT_MAX; |
Hans Verkuil | 3fd8e64 | 2010-09-30 09:29:37 -0300 | [diff] [blame] | 668 | const struct v4l2_frmsize_discrete *size, *best = NULL; |
Guennadi Liakhovetski | 79c6ff9 | 2010-08-27 13:41:44 -0300 | [diff] [blame] | 669 | |
| 670 | if (!probe) |
| 671 | return best; |
| 672 | |
| 673 | for (i = 0, size = probe->sizes; i < probe->num_sizes; i++, size++) { |
| 674 | error = abs(size->width - width) + abs(size->height - height); |
| 675 | if (error < min_error) { |
| 676 | min_error = error; |
| 677 | best = size; |
| 678 | } |
| 679 | if (!error) |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | return best; |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(v4l2_find_nearest_format); |