blob: eb69c52e2f640631493ba8268947e2417c37ad74 [file] [log] [blame]
Hans Verkuil09965172010-08-01 14:32:42 -03001/*
2 V4L2 controls support header.
3
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef _V4L2_CTRLS_H
22#define _V4L2_CTRLS_H
23
24#include <linux/list.h>
Andrzej Hajdaa19dec62013-06-28 05:44:22 -030025#include <linux/mutex.h>
Laurent Pinchart01c40c02010-11-19 11:20:06 -030026#include <linux/videodev2.h>
Hans Verkuil09965172010-08-01 14:32:42 -030027
28/* forward references */
Laurent Pinchart528f0f72012-04-23 08:20:35 -030029struct file;
Hans Verkuil09965172010-08-01 14:32:42 -030030struct v4l2_ctrl_handler;
Hans Verkuileb5b16e2011-06-14 10:04:06 -030031struct v4l2_ctrl_helper;
Hans Verkuil09965172010-08-01 14:32:42 -030032struct v4l2_ctrl;
33struct video_device;
34struct v4l2_subdev;
Hans Verkuil77068d32011-06-13 18:55:58 -030035struct v4l2_subscribed_event;
Hans Verkuil6e239392011-06-07 11:13:44 -030036struct v4l2_fh;
Hans Verkuila26243b2012-01-27 16:18:42 -030037struct poll_table_struct;
Hans Verkuil09965172010-08-01 14:32:42 -030038
Hans Verkuil01760772014-04-27 03:22:17 -030039/** union v4l2_ctrl_ptr - A pointer to a control value.
40 * @p_s32: Pointer to a 32-bit signed value.
41 * @p_s64: Pointer to a 64-bit signed value.
42 * @p_char: Pointer to a string.
43 * @p: Pointer to a compound value.
44 */
45union v4l2_ctrl_ptr {
46 s32 *p_s32;
47 s64 *p_s64;
48 char *p_char;
49 void *p;
50};
51
Hans Verkuil09965172010-08-01 14:32:42 -030052/** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
53 * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
54 * for volatile (and usually read-only) controls such as a control
55 * that returns the current signal strength which changes
56 * continuously.
57 * If not set, then the currently cached value will be returned.
58 * @try_ctrl: Test whether the control's value is valid. Only relevant when
59 * the usual min/max/step checks are not sufficient.
60 * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
61 * ctrl->handler->lock is held when these ops are called, so no
62 * one else can access controls owned by that handler.
63 */
64struct v4l2_ctrl_ops {
65 int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
66 int (*try_ctrl)(struct v4l2_ctrl *ctrl);
67 int (*s_ctrl)(struct v4l2_ctrl *ctrl);
68};
69
Hans Verkuil01760772014-04-27 03:22:17 -030070/** struct v4l2_ctrl_type_ops - The control type operations that the driver has to provide.
71 * @equal: return true if both values are equal.
72 * @init: initialize the value.
73 * @log: log the value.
74 * @validate: validate the value. Return 0 on success and a negative value otherwise.
75 */
76struct v4l2_ctrl_type_ops {
77 bool (*equal)(const struct v4l2_ctrl *ctrl,
78 union v4l2_ctrl_ptr ptr1,
79 union v4l2_ctrl_ptr ptr2);
80 void (*init)(const struct v4l2_ctrl *ctrl,
81 union v4l2_ctrl_ptr ptr);
82 void (*log)(const struct v4l2_ctrl *ctrl);
83 int (*validate)(const struct v4l2_ctrl *ctrl,
84 union v4l2_ctrl_ptr ptr);
85};
86
Hans Verkuil8ac7a942012-09-07 04:46:39 -030087typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
88
Hans Verkuil09965172010-08-01 14:32:42 -030089/** struct v4l2_ctrl - The control structure.
90 * @node: The list node.
Hans Verkuil77068d32011-06-13 18:55:58 -030091 * @ev_subs: The list of control event subscriptions.
Hans Verkuil09965172010-08-01 14:32:42 -030092 * @handler: The handler that owns the control.
93 * @cluster: Point to start of cluster array.
94 * @ncontrols: Number of controls in cluster array.
Hans Verkuil09965172010-08-01 14:32:42 -030095 * @done: Internal flag: set for each processed control.
Hans Verkuil2a863792011-01-11 14:45:03 -030096 * @is_new: Set when the user specified a new value for this control. It
97 * is also set when called from v4l2_ctrl_handler_setup. Drivers
98 * should never set this flag.
Hans Verkuil9ea1b7a2014-01-17 08:25:26 -030099 * @has_changed: Set when the current value differs from the new value. Drivers
100 * should never use this flag.
Hans Verkuil09965172010-08-01 14:32:42 -0300101 * @is_private: If set, then this control is private to its handler and it
102 * will not be added to any other handlers. Drivers can set
103 * this flag.
Hans Verkuil72d877c2011-06-10 05:44:36 -0300104 * @is_auto: If set, then this control selects whether the other cluster
105 * members are in 'automatic' mode or 'manual' mode. This is
106 * used for autogain/gain type clusters. Drivers should never
107 * set this flag directly.
Hans Verkuild9a25472014-06-12 07:54:16 -0300108 * @is_int: If set, then this control has a simple integer value (i.e. it
109 * uses ctrl->val).
110 * @is_string: If set, then this control has type V4L2_CTRL_TYPE_STRING.
111 * @is_ptr: If set, then this control is an array and/or has type >= V4L2_CTRL_COMPOUND_TYPES
112 * and/or has type V4L2_CTRL_TYPE_STRING. In other words, struct
113 * v4l2_ext_control uses field p to point to the data.
Hans Verkuil5626b8c2011-08-26 07:53:53 -0300114 * @has_volatiles: If set, then one or more members of the cluster are volatile.
115 * Drivers should never touch this flag.
Hans Verkuil8ac7a942012-09-07 04:46:39 -0300116 * @call_notify: If set, then call the handler's notify function whenever the
117 * control's value changes.
Hans Verkuil72d877c2011-06-10 05:44:36 -0300118 * @manual_mode_value: If the is_auto flag is set, then this is the value
119 * of the auto control that determines if that control is in
120 * manual mode. So if the value of the auto control equals this
121 * value, then the whole cluster is in manual mode. Drivers should
122 * never set this flag directly.
Hans Verkuil09965172010-08-01 14:32:42 -0300123 * @ops: The control ops.
Hans Verkuil01760772014-04-27 03:22:17 -0300124 * @type_ops: The control type ops.
Hans Verkuil09965172010-08-01 14:32:42 -0300125 * @id: The control ID.
126 * @name: The control name.
127 * @type: The control type.
128 * @minimum: The control's minimum value.
129 * @maximum: The control's maximum value.
130 * @default_value: The control's default value.
131 * @step: The control's step value for non-menu controls.
Hans Verkuild9a25472014-06-12 07:54:16 -0300132 * @elem_size: The size in bytes of the control.
Hans Verkuil09965172010-08-01 14:32:42 -0300133 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
134 * easy to skip menu items that are not valid. If bit X is set,
135 * then menu item X is skipped. Of course, this only works for
136 * menus with <= 32 menu items. There are no menus that come
137 * close to that number, so this is OK. Should we ever need more,
138 * then this will have to be extended to a u64 or a bit array.
139 * @qmenu: A const char * array for all menu items. Array entries that are
140 * empty strings ("") correspond to non-existing menu items (this
141 * is in addition to the menu_skip_mask above). The last entry
142 * must be NULL.
143 * @flags: The control's flags.
144 * @cur: The control's current value.
145 * @val: The control's new s32 value.
146 * @val64: The control's new s64 value.
Hans Verkuil09965172010-08-01 14:32:42 -0300147 * @priv: The control's private pointer. For use by the driver. It is
148 * untouched by the control framework. Note that this pointer is
149 * not freed when the control is deleted. Should this be needed
150 * then a new internal bitfield can be added to tell the framework
151 * to free this pointer.
152 */
153struct v4l2_ctrl {
154 /* Administrative fields */
155 struct list_head node;
Hans Verkuil77068d32011-06-13 18:55:58 -0300156 struct list_head ev_subs;
Hans Verkuil09965172010-08-01 14:32:42 -0300157 struct v4l2_ctrl_handler *handler;
158 struct v4l2_ctrl **cluster;
159 unsigned ncontrols;
Hans Verkuil09965172010-08-01 14:32:42 -0300160 unsigned int done:1;
161
Hans Verkuil2a863792011-01-11 14:45:03 -0300162 unsigned int is_new:1;
Hans Verkuil9ea1b7a2014-01-17 08:25:26 -0300163 unsigned int has_changed:1;
Hans Verkuil09965172010-08-01 14:32:42 -0300164 unsigned int is_private:1;
Hans Verkuil72d877c2011-06-10 05:44:36 -0300165 unsigned int is_auto:1;
Hans Verkuild9a25472014-06-12 07:54:16 -0300166 unsigned int is_int:1;
167 unsigned int is_string:1;
168 unsigned int is_ptr:1;
Hans Verkuil5626b8c2011-08-26 07:53:53 -0300169 unsigned int has_volatiles:1;
Hans Verkuil8ac7a942012-09-07 04:46:39 -0300170 unsigned int call_notify:1;
Hans Verkuil82a7c042011-06-28 10:43:13 -0300171 unsigned int manual_mode_value:8;
Hans Verkuil09965172010-08-01 14:32:42 -0300172
173 const struct v4l2_ctrl_ops *ops;
Hans Verkuil01760772014-04-27 03:22:17 -0300174 const struct v4l2_ctrl_type_ops *type_ops;
Hans Verkuil09965172010-08-01 14:32:42 -0300175 u32 id;
176 const char *name;
177 enum v4l2_ctrl_type type;
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300178 s64 minimum, maximum, default_value;
Hans Verkuild9a25472014-06-12 07:54:16 -0300179 u32 elem_size;
Hans Verkuil09965172010-08-01 14:32:42 -0300180 union {
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300181 u64 step;
182 u64 menu_skip_mask;
Hans Verkuil09965172010-08-01 14:32:42 -0300183 };
Sakari Ailusce580fe2011-08-04 13:51:11 -0300184 union {
185 const char * const *qmenu;
186 const s64 *qmenu_int;
187 };
Hans Verkuil09965172010-08-01 14:32:42 -0300188 unsigned long flags;
Hans Verkuil09965172010-08-01 14:32:42 -0300189 void *priv;
Hans Verkuil2a9ec372014-04-27 03:38:13 -0300190 s32 val;
191 struct {
Hans Verkuild9a25472014-06-12 07:54:16 -0300192 s32 val;
Hans Verkuild9a25472014-06-12 07:54:16 -0300193 } cur;
Hans Verkuil01760772014-04-27 03:22:17 -0300194
195 union v4l2_ctrl_ptr p_new;
196 union v4l2_ctrl_ptr p_cur;
Hans Verkuil09965172010-08-01 14:32:42 -0300197};
198
199/** struct v4l2_ctrl_ref - The control reference.
200 * @node: List node for the sorted list.
201 * @next: Single-link list node for the hash.
202 * @ctrl: The actual control information.
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300203 * @helper: Pointer to helper struct. Used internally in prepare_ext_ctrls().
Hans Verkuil09965172010-08-01 14:32:42 -0300204 *
205 * Each control handler has a list of these refs. The list_head is used to
206 * keep a sorted-by-control-ID list of all controls, while the next pointer
207 * is used to link the control in the hash's bucket.
208 */
209struct v4l2_ctrl_ref {
210 struct list_head node;
211 struct v4l2_ctrl_ref *next;
212 struct v4l2_ctrl *ctrl;
Hans Verkuileb5b16e2011-06-14 10:04:06 -0300213 struct v4l2_ctrl_helper *helper;
Hans Verkuil09965172010-08-01 14:32:42 -0300214};
215
216/** struct v4l2_ctrl_handler - The control handler keeps track of all the
217 * controls: both the controls owned by the handler and those inherited
218 * from other handlers.
Sakari Ailus77e7c4e2012-01-24 21:05:34 -0300219 * @_lock: Default for "lock".
Hans Verkuil09965172010-08-01 14:32:42 -0300220 * @lock: Lock to control access to this handler and its controls.
Sakari Ailus77e7c4e2012-01-24 21:05:34 -0300221 * May be replaced by the user right after init.
Hans Verkuil09965172010-08-01 14:32:42 -0300222 * @ctrls: The list of controls owned by this handler.
223 * @ctrl_refs: The list of control references.
224 * @cached: The last found control reference. It is common that the same
225 * control is needed multiple times, so this is a simple
226 * optimization.
227 * @buckets: Buckets for the hashing. Allows for quick control lookup.
Hans Verkuil8ac7a942012-09-07 04:46:39 -0300228 * @notify: A notify callback that is called whenever the control changes value.
229 * Note that the handler's lock is held when the notify function
230 * is called!
231 * @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
Hans Verkuil09965172010-08-01 14:32:42 -0300232 * @nr_of_buckets: Total number of buckets in the array.
233 * @error: The error code of the first failed control addition.
234 */
235struct v4l2_ctrl_handler {
Sakari Ailus77e7c4e2012-01-24 21:05:34 -0300236 struct mutex _lock;
237 struct mutex *lock;
Hans Verkuil09965172010-08-01 14:32:42 -0300238 struct list_head ctrls;
239 struct list_head ctrl_refs;
240 struct v4l2_ctrl_ref *cached;
241 struct v4l2_ctrl_ref **buckets;
Hans Verkuil8ac7a942012-09-07 04:46:39 -0300242 v4l2_ctrl_notify_fnc notify;
243 void *notify_priv;
Hans Verkuil09965172010-08-01 14:32:42 -0300244 u16 nr_of_buckets;
245 int error;
246};
247
248/** struct v4l2_ctrl_config - Control configuration structure.
249 * @ops: The control ops.
Hans Verkuil01760772014-04-27 03:22:17 -0300250 * @type_ops: The control type ops. Only needed for compound controls.
Hans Verkuil09965172010-08-01 14:32:42 -0300251 * @id: The control ID.
252 * @name: The control name.
253 * @type: The control type.
254 * @min: The control's minimum value.
255 * @max: The control's maximum value.
256 * @step: The control's step value for non-menu controls.
257 * @def: The control's default value.
Hans Verkuild9a25472014-06-12 07:54:16 -0300258 * @elem_size: The size in bytes of the control.
Hans Verkuil09965172010-08-01 14:32:42 -0300259 * @flags: The control's flags.
260 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
261 * easy to skip menu items that are not valid. If bit X is set,
262 * then menu item X is skipped. Of course, this only works for
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300263 * menus with <= 64 menu items. There are no menus that come
Hans Verkuil09965172010-08-01 14:32:42 -0300264 * close to that number, so this is OK. Should we ever need more,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300265 * then this will have to be extended to a bit array.
Hans Verkuil09965172010-08-01 14:32:42 -0300266 * @qmenu: A const char * array for all menu items. Array entries that are
267 * empty strings ("") correspond to non-existing menu items (this
268 * is in addition to the menu_skip_mask above). The last entry
269 * must be NULL.
270 * @is_private: If set, then this control is private to its handler and it
271 * will not be added to any other handlers.
Hans Verkuil09965172010-08-01 14:32:42 -0300272 */
273struct v4l2_ctrl_config {
274 const struct v4l2_ctrl_ops *ops;
Hans Verkuil01760772014-04-27 03:22:17 -0300275 const struct v4l2_ctrl_type_ops *type_ops;
Hans Verkuil09965172010-08-01 14:32:42 -0300276 u32 id;
277 const char *name;
278 enum v4l2_ctrl_type type;
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300279 s64 min;
280 s64 max;
281 u64 step;
282 s64 def;
Hans Verkuild9a25472014-06-12 07:54:16 -0300283 u32 elem_size;
Hans Verkuil09965172010-08-01 14:32:42 -0300284 u32 flags;
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300285 u64 menu_skip_mask;
Hans Verkuil513521e2010-12-29 14:25:52 -0300286 const char * const *qmenu;
Sakari Ailusce580fe2011-08-04 13:51:11 -0300287 const s64 *qmenu_int;
Hans Verkuil09965172010-08-01 14:32:42 -0300288 unsigned int is_private:1;
Hans Verkuil09965172010-08-01 14:32:42 -0300289};
290
291/** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
292 *
293 * This works for all standard V4L2 controls.
294 * For non-standard controls it will only fill in the given arguments
295 * and @name will be NULL.
296 *
297 * This function will overwrite the contents of @name, @type and @flags.
298 * The contents of @min, @max, @step and @def may be modified depending on
299 * the type.
300 *
301 * Do not use in drivers! It is used internally for backwards compatibility
302 * control handling only. Once all drivers are converted to use the new
303 * control framework this function will no longer be exported.
304 */
305void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300306 s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags);
Hans Verkuil09965172010-08-01 14:32:42 -0300307
308
Andy Walls6cd247e2013-03-09 05:55:11 -0300309/** v4l2_ctrl_handler_init_class() - Initialize the control handler.
Hans Verkuil09965172010-08-01 14:32:42 -0300310 * @hdl: The control handler.
311 * @nr_of_controls_hint: A hint of how many controls this handler is
312 * expected to refer to. This is the total number, so including
313 * any inherited controls. It doesn't have to be precise, but if
314 * it is way off, then you either waste memory (too many buckets
315 * are allocated) or the control lookup becomes slower (not enough
316 * buckets are allocated, so there are more slow list lookups).
317 * It will always work, though.
Andy Walls6cd247e2013-03-09 05:55:11 -0300318 * @key: Used by the lock validator if CONFIG_LOCKDEP is set.
319 * @name: Used by the lock validator if CONFIG_LOCKDEP is set.
Hans Verkuil09965172010-08-01 14:32:42 -0300320 *
321 * Returns an error if the buckets could not be allocated. This error will
322 * also be stored in @hdl->error.
Andy Walls6cd247e2013-03-09 05:55:11 -0300323 *
324 * Never use this call directly, always use the v4l2_ctrl_handler_init
325 * macro that hides the @key and @name arguments.
Hans Verkuil09965172010-08-01 14:32:42 -0300326 */
Andy Walls6cd247e2013-03-09 05:55:11 -0300327int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
328 unsigned nr_of_controls_hint,
329 struct lock_class_key *key, const char *name);
330
331#ifdef CONFIG_LOCKDEP
332#define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint) \
333( \
334 ({ \
335 static struct lock_class_key _key; \
336 v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, \
337 &_key, \
338 KBUILD_BASENAME ":" \
339 __stringify(__LINE__) ":" \
340 "(" #hdl ")->_lock"); \
341 }) \
342)
343#else
344#define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint) \
345 v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, NULL, NULL)
346#endif
Hans Verkuil09965172010-08-01 14:32:42 -0300347
348/** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
349 * the control list.
350 * @hdl: The control handler.
351 *
352 * Does nothing if @hdl == NULL.
353 */
354void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
355
356/** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
357 * to the handler to initialize the hardware to the current control values.
358 * @hdl: The control handler.
359 *
360 * Button controls will be skipped, as are read-only controls.
361 *
362 * If @hdl == NULL, then this just returns 0.
363 */
364int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
365
366/** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
367 * @hdl: The control handler.
368 * @prefix: The prefix to use when logging the control values. If the
369 * prefix does not end with a space, then ": " will be added
370 * after the prefix. If @prefix == NULL, then no prefix will be
371 * used.
372 *
373 * For use with VIDIOC_LOG_STATUS.
374 *
375 * Does nothing if @hdl == NULL.
376 */
377void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
378 const char *prefix);
379
380/** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
381 * control.
382 * @hdl: The control handler.
383 * @cfg: The control's configuration data.
384 * @priv: The control's driver-specific private data.
385 *
386 * If the &v4l2_ctrl struct could not be allocated then NULL is returned
387 * and @hdl->error is set to the error code (if it wasn't set already).
388 */
389struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
390 const struct v4l2_ctrl_config *cfg, void *priv);
391
392/** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
393 * @hdl: The control handler.
394 * @ops: The control ops.
395 * @id: The control ID.
396 * @min: The control's minimum value.
397 * @max: The control's maximum value.
398 * @step: The control's step value
399 * @def: The control's default value.
400 *
401 * If the &v4l2_ctrl struct could not be allocated, or the control
402 * ID is not known, then NULL is returned and @hdl->error is set to the
403 * appropriate error code (if it wasn't set already).
404 *
405 * If @id refers to a menu control, then this function will return NULL.
406 *
407 * Use v4l2_ctrl_new_std_menu() when adding menu controls.
408 */
409struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
410 const struct v4l2_ctrl_ops *ops,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300411 u32 id, s64 min, s64 max, u64 step, s64 def);
Hans Verkuil09965172010-08-01 14:32:42 -0300412
413/** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
414 * @hdl: The control handler.
415 * @ops: The control ops.
416 * @id: The control ID.
417 * @max: The control's maximum value.
418 * @mask: The control's skip mask for menu controls. This makes it
419 * easy to skip menu items that are not valid. If bit X is set,
420 * then menu item X is skipped. Of course, this only works for
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300421 * menus with <= 64 menu items. There are no menus that come
Hans Verkuil09965172010-08-01 14:32:42 -0300422 * close to that number, so this is OK. Should we ever need more,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300423 * then this will have to be extended to a bit array.
Hans Verkuil09965172010-08-01 14:32:42 -0300424 * @def: The control's default value.
425 *
426 * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
427 * determines which menu items are to be skipped.
428 *
429 * If @id refers to a non-menu control, then this function will return NULL.
430 */
431struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
432 const struct v4l2_ctrl_ops *ops,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300433 u32 id, u8 max, u64 mask, u8 def);
Hans Verkuil09965172010-08-01 14:32:42 -0300434
Lad, Prabhakar117a7112012-09-18 15:54:38 -0300435/** v4l2_ctrl_new_std_menu_items() - Create a new standard V4L2 menu control
436 * with driver specific menu.
437 * @hdl: The control handler.
438 * @ops: The control ops.
439 * @id: The control ID.
440 * @max: The control's maximum value.
441 * @mask: The control's skip mask for menu controls. This makes it
442 * easy to skip menu items that are not valid. If bit X is set,
443 * then menu item X is skipped. Of course, this only works for
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300444 * menus with <= 64 menu items. There are no menus that come
Lad, Prabhakar117a7112012-09-18 15:54:38 -0300445 * close to that number, so this is OK. Should we ever need more,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300446 * then this will have to be extended to a bit array.
Lad, Prabhakar117a7112012-09-18 15:54:38 -0300447 * @def: The control's default value.
448 * @qmenu: The new menu.
449 *
450 * Same as v4l2_ctrl_new_std_menu(), but @qmenu will be the driver specific
451 * menu of this control.
452 *
453 */
454struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300455 const struct v4l2_ctrl_ops *ops, u32 id, u8 max,
456 u64 mask, u8 def, const char * const *qmenu);
Lad, Prabhakar117a7112012-09-18 15:54:38 -0300457
Sylwester Nawrocki515f3282012-05-06 15:30:44 -0300458/** v4l2_ctrl_new_int_menu() - Create a new standard V4L2 integer menu control.
459 * @hdl: The control handler.
460 * @ops: The control ops.
461 * @id: The control ID.
462 * @max: The control's maximum value.
463 * @def: The control's default value.
464 * @qmenu_int: The control's menu entries.
465 *
466 * Same as v4l2_ctrl_new_std_menu(), but @mask is set to 0 and it additionaly
467 * takes as an argument an array of integers determining the menu items.
468 *
469 * If @id refers to a non-integer-menu control, then this function will return NULL.
470 */
471struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
472 const struct v4l2_ctrl_ops *ops,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300473 u32 id, u8 max, u8 def, const s64 *qmenu_int);
Sylwester Nawrocki515f3282012-05-06 15:30:44 -0300474
Hans Verkuil09965172010-08-01 14:32:42 -0300475/** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
476 * @hdl: The control handler.
477 * @ctrl: The control to add.
478 *
479 * It will return NULL if it was unable to add the control reference.
480 * If the control already belonged to the handler, then it will do
481 * nothing and just return @ctrl.
482 */
483struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
484 struct v4l2_ctrl *ctrl);
485
486/** v4l2_ctrl_add_handler() - Add all controls from handler @add to
487 * handler @hdl.
488 * @hdl: The control handler.
489 * @add: The control handler whose controls you want to add to
490 * the @hdl control handler.
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300491 * @filter: This function will filter which controls should be added.
Hans Verkuil09965172010-08-01 14:32:42 -0300492 *
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300493 * Does nothing if either of the two handlers is a NULL pointer.
494 * If @filter is NULL, then all controls are added. Otherwise only those
495 * controls for which @filter returns true will be added.
Hans Verkuil09965172010-08-01 14:32:42 -0300496 * In case of an error @hdl->error will be set to the error code (if it
497 * wasn't set already).
498 */
499int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300500 struct v4l2_ctrl_handler *add,
501 bool (*filter)(const struct v4l2_ctrl *ctrl));
Hans Verkuil09965172010-08-01 14:32:42 -0300502
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300503/** v4l2_ctrl_radio_filter() - Standard filter for radio controls.
504 * @ctrl: The control that is filtered.
505 *
506 * This will return true for any controls that are valid for radio device
507 * nodes. Those are all of the V4L2_CID_AUDIO_* user controls and all FM
508 * transmitter class controls.
509 *
510 * This function is to be used with v4l2_ctrl_add_handler().
511 */
512bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -0300513
514/** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
515 * @ncontrols: The number of controls in this cluster.
516 * @controls: The cluster control array of size @ncontrols.
517 */
518void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
519
520
Hans Verkuil72d877c2011-06-10 05:44:36 -0300521/** v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
522 * that cluster and set it up for autofoo/foo-type handling.
523 * @ncontrols: The number of controls in this cluster.
524 * @controls: The cluster control array of size @ncontrols. The first control
525 * must be the 'auto' control (e.g. autogain, autoexposure, etc.)
526 * @manual_val: The value for the first control in the cluster that equals the
527 * manual setting.
528 * @set_volatile: If true, then all controls except the first auto control will
Hans Verkuil88365102011-08-26 07:35:14 -0300529 * be volatile.
Hans Verkuil72d877c2011-06-10 05:44:36 -0300530 *
531 * Use for control groups where one control selects some automatic feature and
532 * the other controls are only active whenever the automatic feature is turned
533 * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
534 * red and blue balance, etc.
535 *
536 * The behavior of such controls is as follows:
537 *
538 * When the autofoo control is set to automatic, then any manual controls
539 * are set to inactive and any reads will call g_volatile_ctrl (if the control
540 * was marked volatile).
541 *
542 * When the autofoo control is set to manual, then any manual controls will
543 * be marked active, and any reads will just return the current value without
544 * going through g_volatile_ctrl.
545 *
546 * In addition, this function will set the V4L2_CTRL_FLAG_UPDATE flag
547 * on the autofoo control and V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
548 * if autofoo is in auto mode.
549 */
550void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
551 u8 manual_val, bool set_volatile);
552
553
Hans Verkuil09965172010-08-01 14:32:42 -0300554/** v4l2_ctrl_find() - Find a control with the given ID.
555 * @hdl: The control handler.
556 * @id: The control ID to find.
557 *
558 * If @hdl == NULL this will return NULL as well. Will lock the handler so
559 * do not use from inside &v4l2_ctrl_ops.
560 */
561struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
562
563/** v4l2_ctrl_activate() - Make the control active or inactive.
564 * @ctrl: The control to (de)activate.
565 * @active: True if the control should become active.
566 *
567 * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
568 * Does nothing if @ctrl == NULL.
569 * This will usually be called from within the s_ctrl op.
Hans Verkuil6e239392011-06-07 11:13:44 -0300570 * The V4L2_EVENT_CTRL event will be generated afterwards.
Hans Verkuil09965172010-08-01 14:32:42 -0300571 *
Hans Verkuil6e239392011-06-07 11:13:44 -0300572 * This function assumes that the control handler is locked.
Hans Verkuil09965172010-08-01 14:32:42 -0300573 */
574void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
575
576/** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
577 * @ctrl: The control to (de)activate.
578 * @grabbed: True if the control should become grabbed.
579 *
580 * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
581 * Does nothing if @ctrl == NULL.
Hans Verkuil6e239392011-06-07 11:13:44 -0300582 * The V4L2_EVENT_CTRL event will be generated afterwards.
Hans Verkuil09965172010-08-01 14:32:42 -0300583 * This will usually be called when starting or stopping streaming in the
584 * driver.
585 *
Hans Verkuil6e239392011-06-07 11:13:44 -0300586 * This function assumes that the control handler is not locked and will
587 * take the lock itself.
Hans Verkuil09965172010-08-01 14:32:42 -0300588 */
589void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
590
Sylwester Nawrocki2ccbe772013-01-19 15:51:55 -0300591/** v4l2_ctrl_modify_range() - Update the range of a control.
592 * @ctrl: The control to update.
593 * @min: The control's minimum value.
594 * @max: The control's maximum value.
595 * @step: The control's step value
596 * @def: The control's default value.
597 *
598 * Update the range of a control on the fly. This works for control types
599 * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
600 * @step value is interpreted as a menu_skip_mask.
601 *
602 * An error is returned if one of the range arguments is invalid for this
603 * control type.
604 *
605 * This function assumes that the control handler is not locked and will
606 * take the lock itself.
607 */
608int v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
Hans Verkuil0ba2aeb2014-04-16 09:41:25 -0300609 s64 min, s64 max, u64 step, s64 def);
Sylwester Nawrocki2ccbe772013-01-19 15:51:55 -0300610
Hans Verkuil09965172010-08-01 14:32:42 -0300611/** v4l2_ctrl_lock() - Helper function to lock the handler
612 * associated with the control.
613 * @ctrl: The control to lock.
614 */
615static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
616{
Sakari Ailus77e7c4e2012-01-24 21:05:34 -0300617 mutex_lock(ctrl->handler->lock);
Hans Verkuil09965172010-08-01 14:32:42 -0300618}
619
Frank Schaeferb594a752013-10-10 14:21:32 -0300620/** v4l2_ctrl_unlock() - Helper function to unlock the handler
Hans Verkuil09965172010-08-01 14:32:42 -0300621 * associated with the control.
622 * @ctrl: The control to unlock.
623 */
624static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
625{
Sakari Ailus77e7c4e2012-01-24 21:05:34 -0300626 mutex_unlock(ctrl->handler->lock);
Hans Verkuil09965172010-08-01 14:32:42 -0300627}
628
Hans Verkuil8ac7a942012-09-07 04:46:39 -0300629/** v4l2_ctrl_notify() - Function to set a notify callback for a control.
630 * @ctrl: The control.
631 * @notify: The callback function.
632 * @priv: The callback private handle, passed as argument to the callback.
633 *
634 * This function sets a callback function for the control. If @ctrl is NULL,
635 * then it will do nothing. If @notify is NULL, then the notify callback will
636 * be removed.
637 *
638 * There can be only one notify. If another already exists, then a WARN_ON
639 * will be issued and the function will do nothing.
640 */
641void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv);
642
Hans Verkuil09965172010-08-01 14:32:42 -0300643/** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
644 * @ctrl: The control.
645 *
646 * This returns the control's value safely by going through the control
647 * framework. This function will lock the control's handler, so it cannot be
648 * used from within the &v4l2_ctrl_ops functions.
649 *
650 * This function is for integer type controls only.
651 */
652s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
653
654/** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
655 * @ctrl: The control.
656 * @val: The new value.
657 *
658 * This set the control's new value safely by going through the control
659 * framework. This function will lock the control's handler, so it cannot be
660 * used from within the &v4l2_ctrl_ops functions.
661 *
662 * This function is for integer type controls only.
663 */
664int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
665
Laurent Pinchart03d52852012-07-23 09:15:21 -0300666/** v4l2_ctrl_g_ctrl_int64() - Helper function to get a 64-bit control's value from within a driver.
667 * @ctrl: The control.
668 *
669 * This returns the control's value safely by going through the control
670 * framework. This function will lock the control's handler, so it cannot be
671 * used from within the &v4l2_ctrl_ops functions.
672 *
673 * This function is for 64-bit integer type controls only.
674 */
675s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl);
676
677/** v4l2_ctrl_s_ctrl_int64() - Helper function to set a 64-bit control's value from within a driver.
678 * @ctrl: The control.
679 * @val: The new value.
680 *
681 * This set the control's new value safely by going through the control
682 * framework. This function will lock the control's handler, so it cannot be
683 * used from within the &v4l2_ctrl_ops functions.
684 *
685 * This function is for 64-bit integer type controls only.
686 */
687int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val);
688
Hans Verkuilce727572011-06-10 05:56:39 -0300689/* Internal helper functions that deal with control events. */
Hans de Goede3e3661492012-04-08 12:59:47 -0300690extern const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops;
691void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new);
692void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new);
Hans Verkuil6e239392011-06-07 11:13:44 -0300693
Hans Verkuile2ecb252012-02-02 08:20:53 -0300694/* Can be used as a vidioc_log_status function that just dumps all controls
695 associated with the filehandle. */
696int v4l2_ctrl_log_status(struct file *file, void *fh);
697
Hans Verkuila26243b2012-01-27 16:18:42 -0300698/* Can be used as a vidioc_subscribe_event function that just subscribes
699 control events. */
700int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
Hans Verkuil85f5fe32012-09-04 11:46:09 -0300701 const struct v4l2_event_subscription *sub);
Hans Verkuila26243b2012-01-27 16:18:42 -0300702
703/* Can be used as a poll function that just polls for control events. */
704unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait);
705
Hans Verkuil09965172010-08-01 14:32:42 -0300706/* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
707int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
Hans Verkuild9a25472014-06-12 07:54:16 -0300708int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc);
Hans Verkuil09965172010-08-01 14:32:42 -0300709int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
710int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
Hans Verkuilab892ba2011-06-07 06:47:18 -0300711int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
712 struct v4l2_control *ctrl);
Hans Verkuil09965172010-08-01 14:32:42 -0300713int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
714int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
Hans Verkuilab892ba2011-06-07 06:47:18 -0300715int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
716 struct v4l2_ext_controls *c);
Hans Verkuil09965172010-08-01 14:32:42 -0300717
718/* Helpers for subdevices. If the associated ctrl_handler == NULL then they
719 will all return -EINVAL. */
720int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
721int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
722int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
723int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
724int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
725int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
726int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
727
Sylwester Nawrocki22fa4272013-01-22 19:00:23 -0300728/* Can be used as a subscribe_event function that just subscribes control
729 events. */
730int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
731 struct v4l2_event_subscription *sub);
732
Sylwester Nawrockiffa9b9f2013-01-22 19:01:02 -0300733/* Log all controls owned by subdev's control handler. */
734int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd);
735
Hans Verkuil09965172010-08-01 14:32:42 -0300736#endif