c291dcb1bc625343bd9e6d336e2011be2d8f96b3
[firefly-linux-kernel-4.4.55.git] / include / media / v4l2-ctrls.h
1 /*
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>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h>
27
28 /* forward references */
29 struct file;
30 struct v4l2_ctrl_handler;
31 struct v4l2_ctrl_helper;
32 struct v4l2_ctrl;
33 struct video_device;
34 struct v4l2_subdev;
35 struct v4l2_subscribed_event;
36 struct v4l2_fh;
37 struct poll_table_struct;
38
39 /**
40  * union v4l2_ctrl_ptr - A pointer to a control value.
41  * @p_s32:      Pointer to a 32-bit signed value.
42  * @p_s64:      Pointer to a 64-bit signed value.
43  * @p_u8:       Pointer to a 8-bit unsigned value.
44  * @p_u16:      Pointer to a 16-bit unsigned value.
45  * @p_u32:      Pointer to a 32-bit unsigned value.
46  * @p_char:     Pointer to a string.
47  * @p:          Pointer to a compound value.
48  */
49 union v4l2_ctrl_ptr {
50         s32 *p_s32;
51         s64 *p_s64;
52         u8 *p_u8;
53         u16 *p_u16;
54         u32 *p_u32;
55         char *p_char;
56         void *p;
57 };
58
59 /**
60  * struct v4l2_ctrl_ops - The control operations that the driver has to provide.
61  * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
62  *              for volatile (and usually read-only) controls such as a control
63  *              that returns the current signal strength which changes
64  *              continuously.
65  *              If not set, then the currently cached value will be returned.
66  * @try_ctrl:   Test whether the control's value is valid. Only relevant when
67  *              the usual min/max/step checks are not sufficient.
68  * @s_ctrl:     Actually set the new control value. s_ctrl is compulsory. The
69  *              ctrl->handler->lock is held when these ops are called, so no
70  *              one else can access controls owned by that handler.
71  */
72 struct v4l2_ctrl_ops {
73         int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
74         int (*try_ctrl)(struct v4l2_ctrl *ctrl);
75         int (*s_ctrl)(struct v4l2_ctrl *ctrl);
76 };
77
78 /**
79  * struct v4l2_ctrl_type_ops - The control type operations that the driver
80  *                             has to provide.
81  *
82  * @equal: return true if both values are equal.
83  * @init: initialize the value.
84  * @log: log the value.
85  * @validate: validate the value. Return 0 on success and a negative value otherwise.
86  */
87 struct v4l2_ctrl_type_ops {
88         bool (*equal)(const struct v4l2_ctrl *ctrl, u32 idx,
89                       union v4l2_ctrl_ptr ptr1,
90                       union v4l2_ctrl_ptr ptr2);
91         void (*init)(const struct v4l2_ctrl *ctrl, u32 idx,
92                      union v4l2_ctrl_ptr ptr);
93         void (*log)(const struct v4l2_ctrl *ctrl);
94         int (*validate)(const struct v4l2_ctrl *ctrl, u32 idx,
95                         union v4l2_ctrl_ptr ptr);
96 };
97
98 typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
99
100 /**
101  * struct v4l2_ctrl - The control structure.
102  * @node:       The list node.
103  * @ev_subs:    The list of control event subscriptions.
104  * @handler:    The handler that owns the control.
105  * @cluster:    Point to start of cluster array.
106  * @ncontrols:  Number of controls in cluster array.
107  * @done:       Internal flag: set for each processed control.
108  * @is_new:     Set when the user specified a new value for this control. It
109  *              is also set when called from v4l2_ctrl_handler_setup. Drivers
110  *              should never set this flag.
111  * @has_changed: Set when the current value differs from the new value. Drivers
112  *              should never use this flag.
113  * @is_private: If set, then this control is private to its handler and it
114  *              will not be added to any other handlers. Drivers can set
115  *              this flag.
116  * @is_auto:   If set, then this control selects whether the other cluster
117  *              members are in 'automatic' mode or 'manual' mode. This is
118  *              used for autogain/gain type clusters. Drivers should never
119  *              set this flag directly.
120  * @is_int:    If set, then this control has a simple integer value (i.e. it
121  *              uses ctrl->val).
122  * @is_string: If set, then this control has type V4L2_CTRL_TYPE_STRING.
123  * @is_ptr:     If set, then this control is an array and/or has type >= V4L2_CTRL_COMPOUND_TYPES
124  *              and/or has type V4L2_CTRL_TYPE_STRING. In other words, struct
125  *              v4l2_ext_control uses field p to point to the data.
126  * @is_array: If set, then this control contains an N-dimensional array.
127  * @has_volatiles: If set, then one or more members of the cluster are volatile.
128  *              Drivers should never touch this flag.
129  * @call_notify: If set, then call the handler's notify function whenever the
130  *              control's value changes.
131  * @manual_mode_value: If the is_auto flag is set, then this is the value
132  *              of the auto control that determines if that control is in
133  *              manual mode. So if the value of the auto control equals this
134  *              value, then the whole cluster is in manual mode. Drivers should
135  *              never set this flag directly.
136  * @ops:        The control ops.
137  * @type_ops:   The control type ops.
138  * @id: The control ID.
139  * @name:       The control name.
140  * @type:       The control type.
141  * @minimum:    The control's minimum value.
142  * @maximum:    The control's maximum value.
143  * @default_value: The control's default value.
144  * @step:       The control's step value for non-menu controls.
145  * @elems:      The number of elements in the N-dimensional array.
146  * @elem_size:  The size in bytes of the control.
147  * @dims:       The size of each dimension.
148  * @nr_of_dims:The number of dimensions in @dims.
149  * @max_stores:The maximum number of configuration stores of this control.
150  * @nr_of_stores: The number of allocated configuration stores of this control.
151  * @store:      The configuration store that the control op operates on.
152  * @menu_skip_mask: The control's skip mask for menu controls. This makes it
153  *              easy to skip menu items that are not valid. If bit X is set,
154  *              then menu item X is skipped. Of course, this only works for
155  *              menus with <= 32 menu items. There are no menus that come
156  *              close to that number, so this is OK. Should we ever need more,
157  *              then this will have to be extended to a u64 or a bit array.
158  * @qmenu:      A const char * array for all menu items. Array entries that are
159  *              empty strings ("") correspond to non-existing menu items (this
160  *              is in addition to the menu_skip_mask above). The last entry
161  *              must be NULL.
162  * @flags:      The control's flags.
163  * @cur:        The control's current value.
164  * @val:        The control's new s32 value.
165  * @priv:       The control's private pointer. For use by the driver. It is
166  *              untouched by the control framework. Note that this pointer is
167  *              not freed when the control is deleted. Should this be needed
168  *              then a new internal bitfield can be added to tell the framework
169  *              to free this pointer.
170  * @p_cur:      The control's current value represented via an union with
171  *              provides a standard way of accessing control types
172  *              through a pointer.
173  * @p_new:      The control's new value represented via an union with provides
174  *              a standard way of accessing control types
175  *              through a pointer.
176  */
177 struct v4l2_ctrl {
178         /* Administrative fields */
179         struct list_head node;
180         struct list_head ev_subs;
181         struct v4l2_ctrl_handler *handler;
182         struct v4l2_ctrl **cluster;
183         unsigned ncontrols;
184         unsigned int done:1;
185
186         unsigned int is_new:1;
187         unsigned int has_changed:1;
188         unsigned int is_private:1;
189         unsigned int is_auto:1;
190         unsigned int is_int:1;
191         unsigned int is_string:1;
192         unsigned int is_ptr:1;
193         unsigned int is_array:1;
194         unsigned int has_volatiles:1;
195         unsigned int call_notify:1;
196         unsigned int manual_mode_value:8;
197
198         const struct v4l2_ctrl_ops *ops;
199         const struct v4l2_ctrl_type_ops *type_ops;
200         u32 id;
201         const char *name;
202         enum v4l2_ctrl_type type;
203         s64 minimum, maximum, default_value;
204         u32 elems;
205         u32 elem_size;
206         u32 dims[V4L2_CTRL_MAX_DIMS];
207         u32 nr_of_dims;
208         u16 max_stores;
209         u16 nr_of_stores;
210         u16 store;
211         union {
212                 u64 step;
213                 u64 menu_skip_mask;
214         };
215         union {
216                 const char * const *qmenu;
217                 const s64 *qmenu_int;
218         };
219         unsigned long flags;
220         void *priv;
221         s32 val;
222         struct {
223                 s32 val;
224         } cur;
225
226         union v4l2_ctrl_ptr p_new;
227         union v4l2_ctrl_ptr p_cur;
228         union v4l2_ctrl_ptr *p_stores;
229 };
230
231 /**
232  * struct v4l2_ctrl_ref - The control reference.
233  * @node:       List node for the sorted list.
234  * @next:       Single-link list node for the hash.
235  * @ctrl:       The actual control information.
236  * @helper:     Pointer to helper struct. Used internally in prepare_ext_ctrls().
237  *
238  * Each control handler has a list of these refs. The list_head is used to
239  * keep a sorted-by-control-ID list of all controls, while the next pointer
240  * is used to link the control in the hash's bucket.
241  */
242 struct v4l2_ctrl_ref {
243         struct list_head node;
244         struct v4l2_ctrl_ref *next;
245         struct v4l2_ctrl *ctrl;
246         struct v4l2_ctrl_helper *helper;
247 };
248
249 /**
250  * struct v4l2_ctrl_handler - The control handler keeps track of all the
251  * controls: both the controls owned by the handler and those inherited
252  * from other handlers.
253  * @_lock:      Default for "lock".
254  * @lock:       Lock to control access to this handler and its controls.
255  *              May be replaced by the user right after init.
256  * @ctrls:      The list of controls owned by this handler.
257  * @ctrl_refs:  The list of control references.
258  * @cached:     The last found control reference. It is common that the same
259  *              control is needed multiple times, so this is a simple
260  *              optimization.
261  * @buckets:    Buckets for the hashing. Allows for quick control lookup.
262  * @notify:     A notify callback that is called whenever the control changes value.
263  *              Note that the handler's lock is held when the notify function
264  *              is called!
265  * @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
266  * @nr_of_buckets: Total number of buckets in the array.
267  * @error:      The error code of the first failed control addition.
268  */
269 struct v4l2_ctrl_handler {
270         struct mutex _lock;
271         struct mutex *lock;
272         struct list_head ctrls;
273         struct list_head ctrl_refs;
274         struct v4l2_ctrl_ref *cached;
275         struct v4l2_ctrl_ref **buckets;
276         v4l2_ctrl_notify_fnc notify;
277         void *notify_priv;
278         u16 nr_of_buckets;
279         int error;
280 };
281
282 /**
283  * struct v4l2_ctrl_config - Control configuration structure.
284  * @ops:        The control ops.
285  * @type_ops:   The control type ops. Only needed for compound controls.
286  * @id: The control ID.
287  * @name:       The control name.
288  * @type:       The control type.
289  * @min:        The control's minimum value.
290  * @max:        The control's maximum value.
291  * @step:       The control's step value for non-menu controls.
292  * @def:        The control's default value.
293  * @dims:       The size of each dimension.
294  * @elem_size:  The size in bytes of the control.
295  * @max_stores: The maximum number of stores allowed.
296  * @flags:      The control's flags.
297  * @menu_skip_mask: The control's skip mask for menu controls. This makes it
298  *              easy to skip menu items that are not valid. If bit X is set,
299  *              then menu item X is skipped. Of course, this only works for
300  *              menus with <= 64 menu items. There are no menus that come
301  *              close to that number, so this is OK. Should we ever need more,
302  *              then this will have to be extended to a bit array.
303  * @qmenu:      A const char * array for all menu items. Array entries that are
304  *              empty strings ("") correspond to non-existing menu items (this
305  *              is in addition to the menu_skip_mask above). The last entry
306  *              must be NULL.
307  * @qmenu_int:  A const s64 integer array for all menu items of the type
308  *              V4L2_CTRL_TYPE_INTEGER_MENU.
309  * @is_private: If set, then this control is private to its handler and it
310  *              will not be added to any other handlers.
311  */
312 struct v4l2_ctrl_config {
313         const struct v4l2_ctrl_ops *ops;
314         const struct v4l2_ctrl_type_ops *type_ops;
315         u32 id;
316         const char *name;
317         enum v4l2_ctrl_type type;
318         s64 min;
319         s64 max;
320         u64 step;
321         s64 def;
322         u32 dims[V4L2_CTRL_MAX_DIMS];
323         u32 elem_size;
324         u16 max_stores;
325         u32 flags;
326         u64 menu_skip_mask;
327         const char * const *qmenu;
328         const s64 *qmenu_int;
329         unsigned int is_private:1;
330 };
331
332 /*
333  * v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
334  *
335  * This works for all standard V4L2 controls.
336  * For non-standard controls it will only fill in the given arguments
337  * and @name will be NULL.
338  *
339  * This function will overwrite the contents of @name, @type and @flags.
340  * The contents of @min, @max, @step and @def may be modified depending on
341  * the type.
342  *
343  * Do not use in drivers! It is used internally for backwards compatibility
344  * control handling only. Once all drivers are converted to use the new
345  * control framework this function will no longer be exported.
346  */
347 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
348                     s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags);
349
350
351 /**
352  * v4l2_ctrl_handler_init_class() - Initialize the control handler.
353  * @hdl:        The control handler.
354  * @nr_of_controls_hint: A hint of how many controls this handler is
355  *              expected to refer to. This is the total number, so including
356  *              any inherited controls. It doesn't have to be precise, but if
357  *              it is way off, then you either waste memory (too many buckets
358  *              are allocated) or the control lookup becomes slower (not enough
359  *              buckets are allocated, so there are more slow list lookups).
360  *              It will always work, though.
361  * @key:        Used by the lock validator if CONFIG_LOCKDEP is set.
362  * @name:       Used by the lock validator if CONFIG_LOCKDEP is set.
363  *
364  * Returns an error if the buckets could not be allocated. This error will
365  * also be stored in @hdl->error.
366  *
367  * Never use this call directly, always use the v4l2_ctrl_handler_init
368  * macro that hides the @key and @name arguments.
369  */
370 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
371                                  unsigned nr_of_controls_hint,
372                                  struct lock_class_key *key, const char *name);
373
374 #ifdef CONFIG_LOCKDEP
375 #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint)                \
376 (                                                                       \
377         ({                                                              \
378                 static struct lock_class_key _key;                      \
379                 v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint,  \
380                                         &_key,                          \
381                                         KBUILD_BASENAME ":"             \
382                                         __stringify(__LINE__) ":"       \
383                                         "(" #hdl ")->_lock");           \
384         })                                                              \
385 )
386 #else
387 #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint)                \
388         v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, NULL, NULL)
389 #endif
390
391 /**
392  * v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
393  * the control list.
394  * @hdl:        The control handler.
395  *
396  * Does nothing if @hdl == NULL.
397  */
398 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
399
400 /**
401  * v4l2_ctrl_lock() - Helper function to lock the handler
402  * associated with the control.
403  * @ctrl:       The control to lock.
404  */
405 static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
406 {
407         mutex_lock(ctrl->handler->lock);
408 }
409
410 /**
411  * v4l2_ctrl_unlock() - Helper function to unlock the handler
412  * associated with the control.
413  * @ctrl:       The control to unlock.
414  */
415 static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
416 {
417         mutex_unlock(ctrl->handler->lock);
418 }
419
420 /**
421  * v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
422  * to the handler to initialize the hardware to the current control values.
423  * @hdl:        The control handler.
424  *
425  * Button controls will be skipped, as are read-only controls.
426  *
427  * If @hdl == NULL, then this just returns 0.
428  */
429 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
430
431 /**
432  * v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
433  * @hdl:        The control handler.
434  * @prefix:     The prefix to use when logging the control values. If the
435  *              prefix does not end with a space, then ": " will be added
436  *              after the prefix. If @prefix == NULL, then no prefix will be
437  *              used.
438  *
439  * For use with VIDIOC_LOG_STATUS.
440  *
441  * Does nothing if @hdl == NULL.
442  */
443 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
444                                   const char *prefix);
445
446 /**
447  * v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
448  * control.
449  * @hdl:        The control handler.
450  * @cfg:        The control's configuration data.
451  * @priv:       The control's driver-specific private data.
452  *
453  * If the &v4l2_ctrl struct could not be allocated then NULL is returned
454  * and @hdl->error is set to the error code (if it wasn't set already).
455  */
456 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
457                         const struct v4l2_ctrl_config *cfg, void *priv);
458
459 /**
460  * v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
461  * @hdl:        The control handler.
462  * @ops:        The control ops.
463  * @id: The control ID.
464  * @min:        The control's minimum value.
465  * @max:        The control's maximum value.
466  * @step:       The control's step value
467  * @def:        The control's default value.
468  *
469  * If the &v4l2_ctrl struct could not be allocated, or the control
470  * ID is not known, then NULL is returned and @hdl->error is set to the
471  * appropriate error code (if it wasn't set already).
472  *
473  * If @id refers to a menu control, then this function will return NULL.
474  *
475  * Use v4l2_ctrl_new_std_menu() when adding menu controls.
476  */
477 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
478                         const struct v4l2_ctrl_ops *ops,
479                         u32 id, s64 min, s64 max, u64 step, s64 def);
480
481 /**
482  * v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
483  * @hdl:        The control handler.
484  * @ops:        The control ops.
485  * @id: The control ID.
486  * @max:        The control's maximum value.
487  * @mask:       The control's skip mask for menu controls. This makes it
488  *              easy to skip menu items that are not valid. If bit X is set,
489  *              then menu item X is skipped. Of course, this only works for
490  *              menus with <= 64 menu items. There are no menus that come
491  *              close to that number, so this is OK. Should we ever need more,
492  *              then this will have to be extended to a bit array.
493  * @def:        The control's default value.
494  *
495  * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
496  * determines which menu items are to be skipped.
497  *
498  * If @id refers to a non-menu control, then this function will return NULL.
499  */
500 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
501                         const struct v4l2_ctrl_ops *ops,
502                         u32 id, u8 max, u64 mask, u8 def);
503
504 /**
505  * v4l2_ctrl_new_std_menu_items() - Create a new standard V4L2 menu control
506  * with driver specific menu.
507  * @hdl:        The control handler.
508  * @ops:        The control ops.
509  * @id: The control ID.
510  * @max:        The control's maximum value.
511  * @mask:       The control's skip mask for menu controls. This makes it
512  *              easy to skip menu items that are not valid. If bit X is set,
513  *              then menu item X is skipped. Of course, this only works for
514  *              menus with <= 64 menu items. There are no menus that come
515  *              close to that number, so this is OK. Should we ever need more,
516  *              then this will have to be extended to a bit array.
517  * @def:        The control's default value.
518  * @qmenu:      The new menu.
519  *
520  * Same as v4l2_ctrl_new_std_menu(), but @qmenu will be the driver specific
521  * menu of this control.
522  *
523  */
524 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
525                         const struct v4l2_ctrl_ops *ops, u32 id, u8 max,
526                         u64 mask, u8 def, const char * const *qmenu);
527
528 /**
529  * v4l2_ctrl_new_int_menu() - Create a new standard V4L2 integer menu control.
530  * @hdl:        The control handler.
531  * @ops:        The control ops.
532  * @id: The control ID.
533  * @max:        The control's maximum value.
534  * @def:        The control's default value.
535  * @qmenu_int:  The control's menu entries.
536  *
537  * Same as v4l2_ctrl_new_std_menu(), but @mask is set to 0 and it additionaly
538  * takes as an argument an array of integers determining the menu items.
539  *
540  * If @id refers to a non-integer-menu control, then this function will return NULL.
541  */
542 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
543                         const struct v4l2_ctrl_ops *ops,
544                         u32 id, u8 max, u8 def, const s64 *qmenu_int);
545
546 /**
547  * v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
548  * @hdl:        The control handler.
549  * @ctrl:       The control to add.
550  *
551  * It will return NULL if it was unable to add the control reference.
552  * If the control already belonged to the handler, then it will do
553  * nothing and just return @ctrl.
554  */
555 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
556                                           struct v4l2_ctrl *ctrl);
557
558 /**
559  * v4l2_ctrl_add_handler() - Add all controls from handler @add to
560  * handler @hdl.
561  * @hdl:        The control handler.
562  * @add:        The control handler whose controls you want to add to
563  *              the @hdl control handler.
564  * @filter:     This function will filter which controls should be added.
565  *
566  * Does nothing if either of the two handlers is a NULL pointer.
567  * If @filter is NULL, then all controls are added. Otherwise only those
568  * controls for which @filter returns true will be added.
569  * In case of an error @hdl->error will be set to the error code (if it
570  * wasn't set already).
571  */
572 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
573                           struct v4l2_ctrl_handler *add,
574                           bool (*filter)(const struct v4l2_ctrl *ctrl));
575
576 /**
577  * v4l2_ctrl_radio_filter() - Standard filter for radio controls.
578  * @ctrl:       The control that is filtered.
579  *
580  * This will return true for any controls that are valid for radio device
581  * nodes. Those are all of the V4L2_CID_AUDIO_* user controls and all FM
582  * transmitter class controls.
583  *
584  * This function is to be used with v4l2_ctrl_add_handler().
585  */
586 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl);
587
588 /**
589  * v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
590  * @ncontrols:  The number of controls in this cluster.
591  * @controls:   The cluster control array of size @ncontrols.
592  */
593 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
594
595
596 /**
597  * v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
598  * that cluster and set it up for autofoo/foo-type handling.
599  * @ncontrols:  The number of controls in this cluster.
600  * @controls:   The cluster control array of size @ncontrols. The first control
601  *              must be the 'auto' control (e.g. autogain, autoexposure, etc.)
602  * @manual_val: The value for the first control in the cluster that equals the
603  *              manual setting.
604  * @set_volatile: If true, then all controls except the first auto control will
605  *              be volatile.
606  *
607  * Use for control groups where one control selects some automatic feature and
608  * the other controls are only active whenever the automatic feature is turned
609  * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
610  * red and blue balance, etc.
611  *
612  * The behavior of such controls is as follows:
613  *
614  * When the autofoo control is set to automatic, then any manual controls
615  * are set to inactive and any reads will call g_volatile_ctrl (if the control
616  * was marked volatile).
617  *
618  * When the autofoo control is set to manual, then any manual controls will
619  * be marked active, and any reads will just return the current value without
620  * going through g_volatile_ctrl.
621  *
622  * In addition, this function will set the V4L2_CTRL_FLAG_UPDATE flag
623  * on the autofoo control and V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
624  * if autofoo is in auto mode.
625  */
626 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
627                         u8 manual_val, bool set_volatile);
628
629
630 /**
631  * v4l2_ctrl_find() - Find a control with the given ID.
632  * @hdl:        The control handler.
633  * @id: The control ID to find.
634  *
635  * If @hdl == NULL this will return NULL as well. Will lock the handler so
636  * do not use from inside &v4l2_ctrl_ops.
637  */
638 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
639
640 /**
641  * v4l2_ctrl_activate() - Make the control active or inactive.
642  * @ctrl:       The control to (de)activate.
643  * @active:     True if the control should become active.
644  *
645  * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
646  * Does nothing if @ctrl == NULL.
647  * This will usually be called from within the s_ctrl op.
648  * The V4L2_EVENT_CTRL event will be generated afterwards.
649  *
650  * This function assumes that the control handler is locked.
651  */
652 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
653
654 /**
655  * v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
656  * @ctrl:       The control to (de)activate.
657  * @grabbed:    True if the control should become grabbed.
658  *
659  * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
660  * Does nothing if @ctrl == NULL.
661  * The V4L2_EVENT_CTRL event will be generated afterwards.
662  * This will usually be called when starting or stopping streaming in the
663  * driver.
664  *
665  * This function assumes that the control handler is not locked and will
666  * take the lock itself.
667  */
668 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
669
670
671 /**
672  *__v4l2_ctrl_modify_range() - Unlocked variant of v4l2_ctrl_modify_range()
673  *
674  * @ctrl:       The control to update.
675  * @min:        The control's minimum value.
676  * @max:        The control's maximum value.
677  * @step:       The control's step value
678  * @def:        The control's default value.
679  *
680  * Update the range of a control on the fly. This works for control types
681  * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
682  * @step value is interpreted as a menu_skip_mask.
683  *
684  * An error is returned if one of the range arguments is invalid for this
685  * control type.
686  *
687  * This function assumes that the control handler is not locked and will
688  * take the lock itself.
689  */
690 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
691                              s64 min, s64 max, u64 step, s64 def);
692
693 /**
694  * v4l2_ctrl_modify_range() - Update the range of a control.
695  * @ctrl:       The control to update.
696  * @min:        The control's minimum value.
697  * @max:        The control's maximum value.
698  * @step:       The control's step value
699  * @def:        The control's default value.
700  *
701  * Update the range of a control on the fly. This works for control types
702  * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
703  * @step value is interpreted as a menu_skip_mask.
704  *
705  * An error is returned if one of the range arguments is invalid for this
706  * control type.
707  *
708  * This function assumes that the control handler is not locked and will
709  * take the lock itself.
710  */
711 static inline int v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
712                                          s64 min, s64 max, u64 step, s64 def)
713 {
714         int rval;
715
716         v4l2_ctrl_lock(ctrl);
717         rval = __v4l2_ctrl_modify_range(ctrl, min, max, step, def);
718         v4l2_ctrl_unlock(ctrl);
719
720         return rval;
721 }
722
723 /**
724  * v4l2_ctrl_notify() - Function to set a notify callback for a control.
725  * @ctrl:       The control.
726  * @notify:     The callback function.
727  * @priv:       The callback private handle, passed as argument to the callback.
728  *
729  * This function sets a callback function for the control. If @ctrl is NULL,
730  * then it will do nothing. If @notify is NULL, then the notify callback will
731  * be removed.
732  *
733  * There can be only one notify. If another already exists, then a WARN_ON
734  * will be issued and the function will do nothing.
735  */
736 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv);
737
738 /**
739  * v4l2_ctrl_get_name() - Get the name of the control
740  * @id:         The control ID.
741  *
742  * This function returns the name of the given control ID or NULL if it isn't
743  * a known control.
744  */
745 const char *v4l2_ctrl_get_name(u32 id);
746
747 /**
748  * v4l2_ctrl_get_menu() - Get the menu string array of the control
749  * @id:         The control ID.
750  *
751  * This function returns the NULL-terminated menu string array name of the
752  * given control ID or NULL if it isn't a known menu control.
753  */
754 const char * const *v4l2_ctrl_get_menu(u32 id);
755
756 /**
757  * v4l2_ctrl_get_int_menu() - Get the integer menu array of the control
758  * @id:         The control ID.
759  * @len:        The size of the integer array.
760  *
761  * This function returns the integer array of the given control ID or NULL if it
762  * if it isn't a known integer menu control.
763  */
764 const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len);
765
766 /**
767  * v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
768  * @ctrl:       The control.
769  *
770  * This returns the control's value safely by going through the control
771  * framework. This function will lock the control's handler, so it cannot be
772  * used from within the &v4l2_ctrl_ops functions.
773  *
774  * This function is for integer type controls only.
775  */
776 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
777
778 /**
779  * __v4l2_ctrl_s_ctrl() - Unlocked variant of v4l2_ctrl_s_ctrl().
780  * @ctrl:       The control.
781  * @val:        The new value.
782  *
783  * This set the control's new value safely by going through the control
784  * framework. This function will lock the control's handler, so it cannot be
785  * used from within the &v4l2_ctrl_ops functions.
786  *
787  * This function is for integer type controls only.
788  */
789 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
790
791 /** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
792  * @ctrl:       The control.
793  * @val:        The new value.
794  *
795  * This set the control's new value safely by going through the control
796  * framework. This function will lock the control's handler, so it cannot be
797  * used from within the &v4l2_ctrl_ops functions.
798  *
799  * This function is for integer type controls only.
800  */
801 static inline int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
802 {
803         int rval;
804
805         v4l2_ctrl_lock(ctrl);
806         rval = __v4l2_ctrl_s_ctrl(ctrl, val);
807         v4l2_ctrl_unlock(ctrl);
808
809         return rval;
810 }
811
812 /**
813  * v4l2_ctrl_g_ctrl_int64() - Helper function to get a 64-bit control's value
814  *      from within a driver.
815  * @ctrl:       The control.
816  *
817  * This returns the control's value safely by going through the control
818  * framework. This function will lock the control's handler, so it cannot be
819  * used from within the &v4l2_ctrl_ops functions.
820  *
821  * This function is for 64-bit integer type controls only.
822  */
823 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl);
824
825 /**
826  * __v4l2_ctrl_s_ctrl_int64() - Unlocked variant of v4l2_ctrl_s_ctrl_int64().
827  *
828  * @ctrl:       The control.
829  * @val:        The new value.
830  *
831  * This set the control's new value safely by going through the control
832  * framework. This function will lock the control's handler, so it cannot be
833  * used from within the &v4l2_ctrl_ops functions.
834  *
835  * This function is for 64-bit integer type controls only.
836  */
837 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val);
838
839 /** v4l2_ctrl_s_ctrl_int64() - Helper function to set a 64-bit control's value
840  *      from within a driver.
841  *
842  * @ctrl:       The control.
843  * @val:        The new value.
844  *
845  * This set the control's new value safely by going through the control
846  * framework. This function will lock the control's handler, so it cannot be
847  * used from within the &v4l2_ctrl_ops functions.
848  *
849  * This function is for 64-bit integer type controls only.
850  */
851 static inline int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
852 {
853         int rval;
854
855         v4l2_ctrl_lock(ctrl);
856         rval = __v4l2_ctrl_s_ctrl_int64(ctrl, val);
857         v4l2_ctrl_unlock(ctrl);
858
859         return rval;
860 }
861
862 /** __v4l2_ctrl_s_ctrl_string() - Unlocked variant of v4l2_ctrl_s_ctrl_string().
863  *
864  * @ctrl:       The control.
865  * @s:          The new string.
866  *
867  * This set the control's new string safely by going through the control
868  * framework. This function will lock the control's handler, so it cannot be
869  * used from within the &v4l2_ctrl_ops functions.
870  *
871  * This function is for string type controls only.
872  */
873 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s);
874
875 /** v4l2_ctrl_s_ctrl_string() - Helper function to set a control's string value
876  *       from within a driver.
877  *
878  * @ctrl:       The control.
879  * @s:          The new string.
880  *
881  * This set the control's new string safely by going through the control
882  * framework. This function will lock the control's handler, so it cannot be
883  * used from within the &v4l2_ctrl_ops functions.
884  *
885  * This function is for string type controls only.
886  */
887 static inline int v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
888 {
889         int rval;
890
891         v4l2_ctrl_lock(ctrl);
892         rval = __v4l2_ctrl_s_ctrl_string(ctrl, s);
893         v4l2_ctrl_unlock(ctrl);
894
895         return rval;
896 }
897
898 static inline void v4l2_ctrl_set_max_stores(struct v4l2_ctrl *ctrl, u16 max_stores)
899 {
900         ctrl->max_stores = max_stores;
901 }
902
903 int v4l2_ctrl_apply_store(struct v4l2_ctrl_handler *hdl, unsigned store);
904
905 /* Internal helper functions that deal with control events. */
906 extern const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops;
907 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new);
908 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new);
909
910 /* Can be used as a vidioc_log_status function that just dumps all controls
911    associated with the filehandle. */
912 int v4l2_ctrl_log_status(struct file *file, void *fh);
913
914 /* Can be used as a vidioc_subscribe_event function that just subscribes
915    control events. */
916 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
917                                 const struct v4l2_event_subscription *sub);
918
919 /* Can be used as a poll function that just polls for control events. */
920 unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait);
921
922 /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
923 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
924 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc);
925 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
926 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
927 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
928                                                 struct v4l2_control *ctrl);
929 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
930 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
931 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
932                                                 struct v4l2_ext_controls *c);
933
934 /* Helpers for subdevices. If the associated ctrl_handler == NULL then they
935    will all return -EINVAL. */
936 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
937 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
938 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
939 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
940 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
941 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
942 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
943
944 /* Can be used as a subscribe_event function that just subscribes control
945    events. */
946 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
947                                      struct v4l2_event_subscription *sub);
948
949 /* Log all controls owned by subdev's control handler. */
950 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd);
951
952 #endif