Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / include / media / v4l2-ctrls.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  V4L2 controls support header.
4  *
5  *  Copyright (C) 2010  Hans Verkuil <hverkuil@xs4all.nl>
6  */
7
8 #ifndef _V4L2_CTRLS_H
9 #define _V4L2_CTRLS_H
10
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/videodev2.h>
14 #include <media/media-request.h>
15
16 /*
17  * Include the stateless codec compound control definitions.
18  * This will move to the public headers once this API is fully stable.
19  */
20 #include <media/hevc-ctrls.h>
21
22 /* forward references */
23 struct file;
24 struct poll_table_struct;
25 struct v4l2_ctrl;
26 struct v4l2_ctrl_handler;
27 struct v4l2_ctrl_helper;
28 struct v4l2_fh;
29 struct v4l2_fwnode_device_properties;
30 struct v4l2_subdev;
31 struct v4l2_subscribed_event;
32 struct video_device;
33
34 /**
35  * union v4l2_ctrl_ptr - A pointer to a control value.
36  * @p_s32:                      Pointer to a 32-bit signed value.
37  * @p_s64:                      Pointer to a 64-bit signed value.
38  * @p_u8:                       Pointer to a 8-bit unsigned value.
39  * @p_u16:                      Pointer to a 16-bit unsigned value.
40  * @p_u32:                      Pointer to a 32-bit unsigned value.
41  * @p_char:                     Pointer to a string.
42  * @p_mpeg2_sequence:           Pointer to a MPEG2 sequence structure.
43  * @p_mpeg2_picture:            Pointer to a MPEG2 picture structure.
44  * @p_mpeg2_quantisation:       Pointer to a MPEG2 quantisation data structure.
45  * @p_fwht_params:              Pointer to a FWHT stateless parameters structure.
46  * @p_h264_sps:                 Pointer to a struct v4l2_ctrl_h264_sps.
47  * @p_h264_pps:                 Pointer to a struct v4l2_ctrl_h264_pps.
48  * @p_h264_scaling_matrix:      Pointer to a struct v4l2_ctrl_h264_scaling_matrix.
49  * @p_h264_slice_params:        Pointer to a struct v4l2_ctrl_h264_slice_params.
50  * @p_h264_decode_params:       Pointer to a struct v4l2_ctrl_h264_decode_params.
51  * @p_h264_pred_weights:        Pointer to a struct v4l2_ctrl_h264_pred_weights.
52  * @p_vp8_frame:                Pointer to a VP8 frame params structure.
53  * @p_hevc_sps:                 Pointer to an HEVC sequence parameter set structure.
54  * @p_hevc_pps:                 Pointer to an HEVC picture parameter set structure.
55  * @p_hevc_slice_params:        Pointer to an HEVC slice parameters structure.
56  * @p_hdr10_cll:                Pointer to an HDR10 Content Light Level structure.
57  * @p_hdr10_mastering:          Pointer to an HDR10 Mastering Display structure.
58  * @p_area:                     Pointer to an area.
59  * @p:                          Pointer to a compound value.
60  * @p_const:                    Pointer to a constant compound value.
61  */
62 union v4l2_ctrl_ptr {
63         s32 *p_s32;
64         s64 *p_s64;
65         u8 *p_u8;
66         u16 *p_u16;
67         u32 *p_u32;
68         char *p_char;
69         struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence;
70         struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture;
71         struct v4l2_ctrl_mpeg2_quantisation *p_mpeg2_quantisation;
72         struct v4l2_ctrl_fwht_params *p_fwht_params;
73         struct v4l2_ctrl_h264_sps *p_h264_sps;
74         struct v4l2_ctrl_h264_pps *p_h264_pps;
75         struct v4l2_ctrl_h264_scaling_matrix *p_h264_scaling_matrix;
76         struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
77         struct v4l2_ctrl_h264_decode_params *p_h264_decode_params;
78         struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights;
79         struct v4l2_ctrl_vp8_frame *p_vp8_frame;
80         struct v4l2_ctrl_hevc_sps *p_hevc_sps;
81         struct v4l2_ctrl_hevc_pps *p_hevc_pps;
82         struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
83         struct v4l2_ctrl_hdr10_cll_info *p_hdr10_cll;
84         struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
85         struct v4l2_area *p_area;
86         void *p;
87         const void *p_const;
88 };
89
90 /**
91  * v4l2_ctrl_ptr_create() - Helper function to return a v4l2_ctrl_ptr from a
92  * void pointer
93  * @ptr:        The void pointer
94  */
95 static inline union v4l2_ctrl_ptr v4l2_ctrl_ptr_create(void *ptr)
96 {
97         union v4l2_ctrl_ptr p = { .p = ptr };
98
99         return p;
100 }
101
102 /**
103  * struct v4l2_ctrl_ops - The control operations that the driver has to provide.
104  *
105  * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
106  *              for volatile (and usually read-only) controls such as a control
107  *              that returns the current signal strength which changes
108  *              continuously.
109  *              If not set, then the currently cached value will be returned.
110  * @try_ctrl:   Test whether the control's value is valid. Only relevant when
111  *              the usual min/max/step checks are not sufficient.
112  * @s_ctrl:     Actually set the new control value. s_ctrl is compulsory. The
113  *              ctrl->handler->lock is held when these ops are called, so no
114  *              one else can access controls owned by that handler.
115  */
116 struct v4l2_ctrl_ops {
117         int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
118         int (*try_ctrl)(struct v4l2_ctrl *ctrl);
119         int (*s_ctrl)(struct v4l2_ctrl *ctrl);
120 };
121
122 /**
123  * struct v4l2_ctrl_type_ops - The control type operations that the driver
124  *                             has to provide.
125  *
126  * @equal: return true if both values are equal.
127  * @init: initialize the value.
128  * @log: log the value.
129  * @validate: validate the value. Return 0 on success and a negative value
130  *      otherwise.
131  */
132 struct v4l2_ctrl_type_ops {
133         bool (*equal)(const struct v4l2_ctrl *ctrl, u32 idx,
134                       union v4l2_ctrl_ptr ptr1,
135                       union v4l2_ctrl_ptr ptr2);
136         void (*init)(const struct v4l2_ctrl *ctrl, u32 idx,
137                      union v4l2_ctrl_ptr ptr);
138         void (*log)(const struct v4l2_ctrl *ctrl);
139         int (*validate)(const struct v4l2_ctrl *ctrl, u32 idx,
140                         union v4l2_ctrl_ptr ptr);
141 };
142
143 /**
144  * typedef v4l2_ctrl_notify_fnc - typedef for a notify argument with a function
145  *      that should be called when a control value has changed.
146  *
147  * @ctrl: pointer to struct &v4l2_ctrl
148  * @priv: control private data
149  *
150  * This typedef definition is used as an argument to v4l2_ctrl_notify()
151  * and as an argument at struct &v4l2_ctrl_handler.
152  */
153 typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv);
154
155 /**
156  * struct v4l2_ctrl - The control structure.
157  *
158  * @node:       The list node.
159  * @ev_subs:    The list of control event subscriptions.
160  * @handler:    The handler that owns the control.
161  * @cluster:    Point to start of cluster array.
162  * @ncontrols:  Number of controls in cluster array.
163  * @done:       Internal flag: set for each processed control.
164  * @is_new:     Set when the user specified a new value for this control. It
165  *              is also set when called from v4l2_ctrl_handler_setup(). Drivers
166  *              should never set this flag.
167  * @has_changed: Set when the current value differs from the new value. Drivers
168  *              should never use this flag.
169  * @is_private: If set, then this control is private to its handler and it
170  *              will not be added to any other handlers. Drivers can set
171  *              this flag.
172  * @is_auto:   If set, then this control selects whether the other cluster
173  *              members are in 'automatic' mode or 'manual' mode. This is
174  *              used for autogain/gain type clusters. Drivers should never
175  *              set this flag directly.
176  * @is_int:    If set, then this control has a simple integer value (i.e. it
177  *              uses ctrl->val).
178  * @is_string: If set, then this control has type %V4L2_CTRL_TYPE_STRING.
179  * @is_ptr:     If set, then this control is an array and/or has type >=
180  *              %V4L2_CTRL_COMPOUND_TYPES
181  *              and/or has type %V4L2_CTRL_TYPE_STRING. In other words, &struct
182  *              v4l2_ext_control uses field p to point to the data.
183  * @is_array: If set, then this control contains an N-dimensional array.
184  * @is_dyn_array: If set, then this control contains a dynamically sized 1-dimensional array.
185  *              If this is set, then @is_array is also set.
186  * @has_volatiles: If set, then one or more members of the cluster are volatile.
187  *              Drivers should never touch this flag.
188  * @call_notify: If set, then call the handler's notify function whenever the
189  *              control's value changes.
190  * @manual_mode_value: If the is_auto flag is set, then this is the value
191  *              of the auto control that determines if that control is in
192  *              manual mode. So if the value of the auto control equals this
193  *              value, then the whole cluster is in manual mode. Drivers should
194  *              never set this flag directly.
195  * @ops:        The control ops.
196  * @type_ops:   The control type ops.
197  * @id: The control ID.
198  * @name:       The control name.
199  * @type:       The control type.
200  * @minimum:    The control's minimum value.
201  * @maximum:    The control's maximum value.
202  * @default_value: The control's default value.
203  * @step:       The control's step value for non-menu controls.
204  * @elems:      The number of elements in the N-dimensional array.
205  * @elem_size:  The size in bytes of the control.
206  * @new_elems:  The number of elements in p_new. This is the same as @elems,
207  *              except for dynamic arrays. In that case it is in the range of
208  *              1 to @p_dyn_alloc_elems.
209  * @dims:       The size of each dimension.
210  * @nr_of_dims:The number of dimensions in @dims.
211  * @menu_skip_mask: The control's skip mask for menu controls. This makes it
212  *              easy to skip menu items that are not valid. If bit X is set,
213  *              then menu item X is skipped. Of course, this only works for
214  *              menus with <= 32 menu items. There are no menus that come
215  *              close to that number, so this is OK. Should we ever need more,
216  *              then this will have to be extended to a u64 or a bit array.
217  * @qmenu:      A const char * array for all menu items. Array entries that are
218  *              empty strings ("") correspond to non-existing menu items (this
219  *              is in addition to the menu_skip_mask above). The last entry
220  *              must be NULL.
221  *              Used only if the @type is %V4L2_CTRL_TYPE_MENU.
222  * @qmenu_int:  A 64-bit integer array for with integer menu items.
223  *              The size of array must be equal to the menu size, e. g.:
224  *              :math:`ceil(\frac{maximum - minimum}{step}) + 1`.
225  *              Used only if the @type is %V4L2_CTRL_TYPE_INTEGER_MENU.
226  * @flags:      The control's flags.
227  * @priv:       The control's private pointer. For use by the driver. It is
228  *              untouched by the control framework. Note that this pointer is
229  *              not freed when the control is deleted. Should this be needed
230  *              then a new internal bitfield can be added to tell the framework
231  *              to free this pointer.
232  * @p_dyn:      Pointer to the dynamically allocated array. Only valid if
233  *              @is_dyn_array is true.
234  * @p_dyn_alloc_elems: The number of elements in the dynamically allocated
235  *              array for both the cur and new values. So @p_dyn is actually
236  *              sized for 2 * @p_dyn_alloc_elems * @elem_size. Only valid if
237  *              @is_dyn_array is true.
238  * @cur:        Structure to store the current value.
239  * @cur.val:    The control's current value, if the @type is represented via
240  *              a u32 integer (see &enum v4l2_ctrl_type).
241  * @val:        The control's new s32 value.
242  * @p_def:      The control's default value represented via a union which
243  *              provides a standard way of accessing control types
244  *              through a pointer (for compound controls only).
245  * @p_cur:      The control's current value represented via a union which
246  *              provides a standard way of accessing control types
247  *              through a pointer.
248  * @p_new:      The control's new value represented via a union which provides
249  *              a standard way of accessing control types
250  *              through a pointer.
251  */
252 struct v4l2_ctrl {
253         /* Administrative fields */
254         struct list_head node;
255         struct list_head ev_subs;
256         struct v4l2_ctrl_handler *handler;
257         struct v4l2_ctrl **cluster;
258         unsigned int ncontrols;
259
260         unsigned int done:1;
261
262         unsigned int is_new:1;
263         unsigned int has_changed:1;
264         unsigned int is_private:1;
265         unsigned int is_auto:1;
266         unsigned int is_int:1;
267         unsigned int is_string:1;
268         unsigned int is_ptr:1;
269         unsigned int is_array:1;
270         unsigned int is_dyn_array:1;
271         unsigned int has_volatiles:1;
272         unsigned int call_notify:1;
273         unsigned int manual_mode_value:8;
274
275         const struct v4l2_ctrl_ops *ops;
276         const struct v4l2_ctrl_type_ops *type_ops;
277         u32 id;
278         const char *name;
279         enum v4l2_ctrl_type type;
280         s64 minimum, maximum, default_value;
281         u32 elems;
282         u32 elem_size;
283         u32 new_elems;
284         u32 dims[V4L2_CTRL_MAX_DIMS];
285         u32 nr_of_dims;
286         union {
287                 u64 step;
288                 u64 menu_skip_mask;
289         };
290         union {
291                 const char * const *qmenu;
292                 const s64 *qmenu_int;
293         };
294         unsigned long flags;
295         void *priv;
296         void *p_dyn;
297         u32 p_dyn_alloc_elems;
298         s32 val;
299         struct {
300                 s32 val;
301         } cur;
302
303         union v4l2_ctrl_ptr p_def;
304         union v4l2_ctrl_ptr p_new;
305         union v4l2_ctrl_ptr p_cur;
306 };
307
308 /**
309  * struct v4l2_ctrl_ref - The control reference.
310  *
311  * @node:       List node for the sorted list.
312  * @next:       Single-link list node for the hash.
313  * @ctrl:       The actual control information.
314  * @helper:     Pointer to helper struct. Used internally in
315  *              ``prepare_ext_ctrls`` function at ``v4l2-ctrl.c``.
316  * @from_other_dev: If true, then @ctrl was defined in another
317  *              device than the &struct v4l2_ctrl_handler.
318  * @req_done:   Internal flag: if the control handler containing this control
319  *              reference is bound to a media request, then this is set when
320  *              the control has been applied. This prevents applying controls
321  *              from a cluster with multiple controls twice (when the first
322  *              control of a cluster is applied, they all are).
323  * @p_req_valid: If set, then p_req contains the control value for the request.
324  * @p_req_dyn_enomem: If set, then p_req is invalid since allocating space for
325  *              a dynamic array failed. Attempting to read this value shall
326  *              result in ENOMEM. Only valid if ctrl->is_dyn_array is true.
327  * @p_req_dyn_alloc_elems: The number of elements allocated for the dynamic
328  *              array. Only valid if @p_req_valid and ctrl->is_dyn_array are
329  *              true.
330  * @p_req_elems: The number of elements in @p_req. This is the same as
331  *              ctrl->elems, except for dynamic arrays. In that case it is in
332  *              the range of 1 to @p_req_dyn_alloc_elems. Only valid if
333  *              @p_req_valid is true.
334  * @p_req:      If the control handler containing this control reference
335  *              is bound to a media request, then this points to the
336  *              value of the control that must be applied when the request
337  *              is executed, or to the value of the control at the time
338  *              that the request was completed. If @p_req_valid is false,
339  *              then this control was never set for this request and the
340  *              control will not be updated when this request is applied.
341  *
342  * Each control handler has a list of these refs. The list_head is used to
343  * keep a sorted-by-control-ID list of all controls, while the next pointer
344  * is used to link the control in the hash's bucket.
345  */
346 struct v4l2_ctrl_ref {
347         struct list_head node;
348         struct v4l2_ctrl_ref *next;
349         struct v4l2_ctrl *ctrl;
350         struct v4l2_ctrl_helper *helper;
351         bool from_other_dev;
352         bool req_done;
353         bool p_req_valid;
354         bool p_req_dyn_enomem;
355         u32 p_req_dyn_alloc_elems;
356         u32 p_req_elems;
357         union v4l2_ctrl_ptr p_req;
358 };
359
360 /**
361  * struct v4l2_ctrl_handler - The control handler keeps track of all the
362  *      controls: both the controls owned by the handler and those inherited
363  *      from other handlers.
364  *
365  * @_lock:      Default for "lock".
366  * @lock:       Lock to control access to this handler and its controls.
367  *              May be replaced by the user right after init.
368  * @ctrls:      The list of controls owned by this handler.
369  * @ctrl_refs:  The list of control references.
370  * @cached:     The last found control reference. It is common that the same
371  *              control is needed multiple times, so this is a simple
372  *              optimization.
373  * @buckets:    Buckets for the hashing. Allows for quick control lookup.
374  * @notify:     A notify callback that is called whenever the control changes
375  *              value.
376  *              Note that the handler's lock is held when the notify function
377  *              is called!
378  * @notify_priv: Passed as argument to the v4l2_ctrl notify callback.
379  * @nr_of_buckets: Total number of buckets in the array.
380  * @error:      The error code of the first failed control addition.
381  * @request_is_queued: True if the request was queued.
382  * @requests:   List to keep track of open control handler request objects.
383  *              For the parent control handler (@req_obj.ops == NULL) this
384  *              is the list header. When the parent control handler is
385  *              removed, it has to unbind and put all these requests since
386  *              they refer to the parent.
387  * @requests_queued: List of the queued requests. This determines the order
388  *              in which these controls are applied. Once the request is
389  *              completed it is removed from this list.
390  * @req_obj:    The &struct media_request_object, used to link into a
391  *              &struct media_request. This request object has a refcount.
392  */
393 struct v4l2_ctrl_handler {
394         struct mutex _lock;
395         struct mutex *lock;
396         struct list_head ctrls;
397         struct list_head ctrl_refs;
398         struct v4l2_ctrl_ref *cached;
399         struct v4l2_ctrl_ref **buckets;
400         v4l2_ctrl_notify_fnc notify;
401         void *notify_priv;
402         u16 nr_of_buckets;
403         int error;
404         bool request_is_queued;
405         struct list_head requests;
406         struct list_head requests_queued;
407         struct media_request_object req_obj;
408 };
409
410 /**
411  * struct v4l2_ctrl_config - Control configuration structure.
412  *
413  * @ops:        The control ops.
414  * @type_ops:   The control type ops. Only needed for compound controls.
415  * @id: The control ID.
416  * @name:       The control name.
417  * @type:       The control type.
418  * @min:        The control's minimum value.
419  * @max:        The control's maximum value.
420  * @step:       The control's step value for non-menu controls.
421  * @def:        The control's default value.
422  * @p_def:      The control's default value for compound controls.
423  * @dims:       The size of each dimension.
424  * @elem_size:  The size in bytes of the control.
425  * @flags:      The control's flags.
426  * @menu_skip_mask: The control's skip mask for menu controls. This makes it
427  *              easy to skip menu items that are not valid. If bit X is set,
428  *              then menu item X is skipped. Of course, this only works for
429  *              menus with <= 64 menu items. There are no menus that come
430  *              close to that number, so this is OK. Should we ever need more,
431  *              then this will have to be extended to a bit array.
432  * @qmenu:      A const char * array for all menu items. Array entries that are
433  *              empty strings ("") correspond to non-existing menu items (this
434  *              is in addition to the menu_skip_mask above). The last entry
435  *              must be NULL.
436  * @qmenu_int:  A const s64 integer array for all menu items of the type
437  *              V4L2_CTRL_TYPE_INTEGER_MENU.
438  * @is_private: If set, then this control is private to its handler and it
439  *              will not be added to any other handlers.
440  */
441 struct v4l2_ctrl_config {
442         const struct v4l2_ctrl_ops *ops;
443         const struct v4l2_ctrl_type_ops *type_ops;
444         u32 id;
445         const char *name;
446         enum v4l2_ctrl_type type;
447         s64 min;
448         s64 max;
449         u64 step;
450         s64 def;
451         union v4l2_ctrl_ptr p_def;
452         u32 dims[V4L2_CTRL_MAX_DIMS];
453         u32 elem_size;
454         u32 flags;
455         u64 menu_skip_mask;
456         const char * const *qmenu;
457         const s64 *qmenu_int;
458         unsigned int is_private:1;
459 };
460
461 /**
462  * v4l2_ctrl_fill - Fill in the control fields based on the control ID.
463  *
464  * @id: ID of the control
465  * @name: pointer to be filled with a string with the name of the control
466  * @type: pointer for storing the type of the control
467  * @min: pointer for storing the minimum value for the control
468  * @max: pointer for storing the maximum value for the control
469  * @step: pointer for storing the control step
470  * @def: pointer for storing the default value for the control
471  * @flags: pointer for storing the flags to be used on the control
472  *
473  * This works for all standard V4L2 controls.
474  * For non-standard controls it will only fill in the given arguments
475  * and @name content will be set to %NULL.
476  *
477  * This function will overwrite the contents of @name, @type and @flags.
478  * The contents of @min, @max, @step and @def may be modified depending on
479  * the type.
480  *
481  * .. note::
482  *
483  *    Do not use in drivers! It is used internally for backwards compatibility
484  *    control handling only. Once all drivers are converted to use the new
485  *    control framework this function will no longer be exported.
486  */
487 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
488                     s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags);
489
490
491 /**
492  * v4l2_ctrl_handler_init_class() - Initialize the control handler.
493  * @hdl:        The control handler.
494  * @nr_of_controls_hint: A hint of how many controls this handler is
495  *              expected to refer to. This is the total number, so including
496  *              any inherited controls. It doesn't have to be precise, but if
497  *              it is way off, then you either waste memory (too many buckets
498  *              are allocated) or the control lookup becomes slower (not enough
499  *              buckets are allocated, so there are more slow list lookups).
500  *              It will always work, though.
501  * @key:        Used by the lock validator if CONFIG_LOCKDEP is set.
502  * @name:       Used by the lock validator if CONFIG_LOCKDEP is set.
503  *
504  * .. attention::
505  *
506  *    Never use this call directly, always use the v4l2_ctrl_handler_init()
507  *    macro that hides the @key and @name arguments.
508  *
509  * Return: returns an error if the buckets could not be allocated. This
510  * error will also be stored in @hdl->error.
511  */
512 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
513                                  unsigned int nr_of_controls_hint,
514                                  struct lock_class_key *key, const char *name);
515
516 #ifdef CONFIG_LOCKDEP
517
518 /**
519  * v4l2_ctrl_handler_init - helper function to create a static struct
520  *       &lock_class_key and calls v4l2_ctrl_handler_init_class()
521  *
522  * @hdl:        The control handler.
523  * @nr_of_controls_hint: A hint of how many controls this handler is
524  *              expected to refer to. This is the total number, so including
525  *              any inherited controls. It doesn't have to be precise, but if
526  *              it is way off, then you either waste memory (too many buckets
527  *              are allocated) or the control lookup becomes slower (not enough
528  *              buckets are allocated, so there are more slow list lookups).
529  *              It will always work, though.
530  *
531  * This helper function creates a static struct &lock_class_key and
532  * calls v4l2_ctrl_handler_init_class(), providing a proper name for the lock
533  * validador.
534  *
535  * Use this helper function to initialize a control handler.
536  */
537 #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint)                \
538 (                                                                       \
539         ({                                                              \
540                 static struct lock_class_key _key;                      \
541                 v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint,  \
542                                         &_key,                          \
543                                         KBUILD_BASENAME ":"             \
544                                         __stringify(__LINE__) ":"       \
545                                         "(" #hdl ")->_lock");           \
546         })                                                              \
547 )
548 #else
549 #define v4l2_ctrl_handler_init(hdl, nr_of_controls_hint)                \
550         v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, NULL, NULL)
551 #endif
552
553 /**
554  * v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
555  * the control list.
556  * @hdl:        The control handler.
557  *
558  * Does nothing if @hdl == NULL.
559  */
560 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
561
562 /**
563  * v4l2_ctrl_lock() - Helper function to lock the handler
564  * associated with the control.
565  * @ctrl:       The control to lock.
566  */
567 static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
568 {
569         mutex_lock(ctrl->handler->lock);
570 }
571
572 /**
573  * v4l2_ctrl_unlock() - Helper function to unlock the handler
574  * associated with the control.
575  * @ctrl:       The control to unlock.
576  */
577 static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
578 {
579         mutex_unlock(ctrl->handler->lock);
580 }
581
582 /**
583  * __v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
584  * to the handler to initialize the hardware to the current control values. The
585  * caller is responsible for acquiring the control handler mutex on behalf of
586  * __v4l2_ctrl_handler_setup().
587  * @hdl:        The control handler.
588  *
589  * Button controls will be skipped, as are read-only controls.
590  *
591  * If @hdl == NULL, then this just returns 0.
592  */
593 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
594
595 /**
596  * v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
597  * to the handler to initialize the hardware to the current control values.
598  * @hdl:        The control handler.
599  *
600  * Button controls will be skipped, as are read-only controls.
601  *
602  * If @hdl == NULL, then this just returns 0.
603  */
604 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
605
606 /**
607  * v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
608  * @hdl:        The control handler.
609  * @prefix:     The prefix to use when logging the control values. If the
610  *              prefix does not end with a space, then ": " will be added
611  *              after the prefix. If @prefix == NULL, then no prefix will be
612  *              used.
613  *
614  * For use with VIDIOC_LOG_STATUS.
615  *
616  * Does nothing if @hdl == NULL.
617  */
618 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
619                                   const char *prefix);
620
621 /**
622  * v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
623  *      control.
624  *
625  * @hdl:        The control handler.
626  * @cfg:        The control's configuration data.
627  * @priv:       The control's driver-specific private data.
628  *
629  * If the &v4l2_ctrl struct could not be allocated then NULL is returned
630  * and @hdl->error is set to the error code (if it wasn't set already).
631  */
632 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
633                                        const struct v4l2_ctrl_config *cfg,
634                                        void *priv);
635
636 /**
637  * v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu
638  *      control.
639  *
640  * @hdl:        The control handler.
641  * @ops:        The control ops.
642  * @id:         The control ID.
643  * @min:        The control's minimum value.
644  * @max:        The control's maximum value.
645  * @step:       The control's step value
646  * @def:        The control's default value.
647  *
648  * If the &v4l2_ctrl struct could not be allocated, or the control
649  * ID is not known, then NULL is returned and @hdl->error is set to the
650  * appropriate error code (if it wasn't set already).
651  *
652  * If @id refers to a menu control, then this function will return NULL.
653  *
654  * Use v4l2_ctrl_new_std_menu() when adding menu controls.
655  */
656 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
657                                     const struct v4l2_ctrl_ops *ops,
658                                     u32 id, s64 min, s64 max, u64 step,
659                                     s64 def);
660
661 /**
662  * v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2
663  *      menu control.
664  *
665  * @hdl:        The control handler.
666  * @ops:        The control ops.
667  * @id:         The control ID.
668  * @max:        The control's maximum value.
669  * @mask:       The control's skip mask for menu controls. This makes it
670  *              easy to skip menu items that are not valid. If bit X is set,
671  *              then menu item X is skipped. Of course, this only works for
672  *              menus with <= 64 menu items. There are no menus that come
673  *              close to that number, so this is OK. Should we ever need more,
674  *              then this will have to be extended to a bit array.
675  * @def:        The control's default value.
676  *
677  * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
678  * determines which menu items are to be skipped.
679  *
680  * If @id refers to a non-menu control, then this function will return NULL.
681  */
682 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
683                                          const struct v4l2_ctrl_ops *ops,
684                                          u32 id, u8 max, u64 mask, u8 def);
685
686 /**
687  * v4l2_ctrl_new_std_menu_items() - Create a new standard V4L2 menu control
688  *      with driver specific menu.
689  *
690  * @hdl:        The control handler.
691  * @ops:        The control ops.
692  * @id: The control ID.
693  * @max:        The control's maximum value.
694  * @mask:       The control's skip mask for menu controls. This makes it
695  *              easy to skip menu items that are not valid. If bit X is set,
696  *              then menu item X is skipped. Of course, this only works for
697  *              menus with <= 64 menu items. There are no menus that come
698  *              close to that number, so this is OK. Should we ever need more,
699  *              then this will have to be extended to a bit array.
700  * @def:        The control's default value.
701  * @qmenu:      The new menu.
702  *
703  * Same as v4l2_ctrl_new_std_menu(), but @qmenu will be the driver specific
704  * menu of this control.
705  *
706  */
707 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
708                                                const struct v4l2_ctrl_ops *ops,
709                                                u32 id, u8 max,
710                                                u64 mask, u8 def,
711                                                const char * const *qmenu);
712
713 /**
714  * v4l2_ctrl_new_std_compound() - Allocate and initialize a new standard V4L2
715  *      compound control.
716  *
717  * @hdl:       The control handler.
718  * @ops:       The control ops.
719  * @id:        The control ID.
720  * @p_def:     The control's default value.
721  *
722  * Sames as v4l2_ctrl_new_std(), but with support to compound controls, thanks
723  * to the @p_def field. Use v4l2_ctrl_ptr_create() to create @p_def from a
724  * pointer. Use v4l2_ctrl_ptr_create(NULL) if the default value of the
725  * compound control should be all zeroes.
726  *
727  */
728 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
729                                              const struct v4l2_ctrl_ops *ops,
730                                              u32 id,
731                                              const union v4l2_ctrl_ptr p_def);
732
733 /**
734  * v4l2_ctrl_new_int_menu() - Create a new standard V4L2 integer menu control.
735  *
736  * @hdl:        The control handler.
737  * @ops:        The control ops.
738  * @id: The control ID.
739  * @max:        The control's maximum value.
740  * @def:        The control's default value.
741  * @qmenu_int:  The control's menu entries.
742  *
743  * Same as v4l2_ctrl_new_std_menu(), but @mask is set to 0 and it additionally
744  * takes as an argument an array of integers determining the menu items.
745  *
746  * If @id refers to a non-integer-menu control, then this function will
747  * return %NULL.
748  */
749 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
750                                          const struct v4l2_ctrl_ops *ops,
751                                          u32 id, u8 max, u8 def,
752                                          const s64 *qmenu_int);
753
754 /**
755  * typedef v4l2_ctrl_filter - Typedef to define the filter function to be
756  *      used when adding a control handler.
757  *
758  * @ctrl: pointer to struct &v4l2_ctrl.
759  */
760
761 typedef bool (*v4l2_ctrl_filter)(const struct v4l2_ctrl *ctrl);
762
763 /**
764  * v4l2_ctrl_add_handler() - Add all controls from handler @add to
765  *      handler @hdl.
766  *
767  * @hdl:        The control handler.
768  * @add:        The control handler whose controls you want to add to
769  *              the @hdl control handler.
770  * @filter:     This function will filter which controls should be added.
771  * @from_other_dev: If true, then the controls in @add were defined in another
772  *              device than @hdl.
773  *
774  * Does nothing if either of the two handlers is a NULL pointer.
775  * If @filter is NULL, then all controls are added. Otherwise only those
776  * controls for which @filter returns true will be added.
777  * In case of an error @hdl->error will be set to the error code (if it
778  * wasn't set already).
779  */
780 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
781                           struct v4l2_ctrl_handler *add,
782                           v4l2_ctrl_filter filter,
783                           bool from_other_dev);
784
785 /**
786  * v4l2_ctrl_radio_filter() - Standard filter for radio controls.
787  *
788  * @ctrl:       The control that is filtered.
789  *
790  * This will return true for any controls that are valid for radio device
791  * nodes. Those are all of the V4L2_CID_AUDIO_* user controls and all FM
792  * transmitter class controls.
793  *
794  * This function is to be used with v4l2_ctrl_add_handler().
795  */
796 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl);
797
798 /**
799  * v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging
800  *      to that cluster.
801  *
802  * @ncontrols:  The number of controls in this cluster.
803  * @controls:   The cluster control array of size @ncontrols.
804  */
805 void v4l2_ctrl_cluster(unsigned int ncontrols, struct v4l2_ctrl **controls);
806
807
808 /**
809  * v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging
810  *      to that cluster and set it up for autofoo/foo-type handling.
811  *
812  * @ncontrols:  The number of controls in this cluster.
813  * @controls:   The cluster control array of size @ncontrols. The first control
814  *              must be the 'auto' control (e.g. autogain, autoexposure, etc.)
815  * @manual_val: The value for the first control in the cluster that equals the
816  *              manual setting.
817  * @set_volatile: If true, then all controls except the first auto control will
818  *              be volatile.
819  *
820  * Use for control groups where one control selects some automatic feature and
821  * the other controls are only active whenever the automatic feature is turned
822  * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
823  * red and blue balance, etc.
824  *
825  * The behavior of such controls is as follows:
826  *
827  * When the autofoo control is set to automatic, then any manual controls
828  * are set to inactive and any reads will call g_volatile_ctrl (if the control
829  * was marked volatile).
830  *
831  * When the autofoo control is set to manual, then any manual controls will
832  * be marked active, and any reads will just return the current value without
833  * going through g_volatile_ctrl.
834  *
835  * In addition, this function will set the %V4L2_CTRL_FLAG_UPDATE flag
836  * on the autofoo control and %V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
837  * if autofoo is in auto mode.
838  */
839 void v4l2_ctrl_auto_cluster(unsigned int ncontrols,
840                             struct v4l2_ctrl **controls,
841                             u8 manual_val, bool set_volatile);
842
843
844 /**
845  * v4l2_ctrl_find() - Find a control with the given ID.
846  *
847  * @hdl:        The control handler.
848  * @id: The control ID to find.
849  *
850  * If @hdl == NULL this will return NULL as well. Will lock the handler so
851  * do not use from inside &v4l2_ctrl_ops.
852  */
853 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
854
855 /**
856  * v4l2_ctrl_activate() - Make the control active or inactive.
857  * @ctrl:       The control to (de)activate.
858  * @active:     True if the control should become active.
859  *
860  * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
861  * Does nothing if @ctrl == NULL.
862  * This will usually be called from within the s_ctrl op.
863  * The V4L2_EVENT_CTRL event will be generated afterwards.
864  *
865  * This function assumes that the control handler is locked.
866  */
867 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
868
869 /**
870  * __v4l2_ctrl_grab() - Unlocked variant of v4l2_ctrl_grab.
871  *
872  * @ctrl:       The control to (de)activate.
873  * @grabbed:    True if the control should become grabbed.
874  *
875  * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
876  * Does nothing if @ctrl == NULL.
877  * The V4L2_EVENT_CTRL event will be generated afterwards.
878  * This will usually be called when starting or stopping streaming in the
879  * driver.
880  *
881  * This function assumes that the control handler is locked by the caller.
882  */
883 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
884
885 /**
886  * v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
887  *
888  * @ctrl:       The control to (de)activate.
889  * @grabbed:    True if the control should become grabbed.
890  *
891  * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
892  * Does nothing if @ctrl == NULL.
893  * The V4L2_EVENT_CTRL event will be generated afterwards.
894  * This will usually be called when starting or stopping streaming in the
895  * driver.
896  *
897  * This function assumes that the control handler is not locked and will
898  * take the lock itself.
899  */
900 static inline void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
901 {
902         if (!ctrl)
903                 return;
904
905         v4l2_ctrl_lock(ctrl);
906         __v4l2_ctrl_grab(ctrl, grabbed);
907         v4l2_ctrl_unlock(ctrl);
908 }
909
910 /**
911  *__v4l2_ctrl_modify_range() - Unlocked variant of v4l2_ctrl_modify_range()
912  *
913  * @ctrl:       The control to update.
914  * @min:        The control's minimum value.
915  * @max:        The control's maximum value.
916  * @step:       The control's step value
917  * @def:        The control's default value.
918  *
919  * Update the range of a control on the fly. This works for control types
920  * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
921  * @step value is interpreted as a menu_skip_mask.
922  *
923  * An error is returned if one of the range arguments is invalid for this
924  * control type.
925  *
926  * The caller is responsible for acquiring the control handler mutex on behalf
927  * of __v4l2_ctrl_modify_range().
928  */
929 int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
930                              s64 min, s64 max, u64 step, s64 def);
931
932 /**
933  * v4l2_ctrl_modify_range() - Update the range of a control.
934  *
935  * @ctrl:       The control to update.
936  * @min:        The control's minimum value.
937  * @max:        The control's maximum value.
938  * @step:       The control's step value
939  * @def:        The control's default value.
940  *
941  * Update the range of a control on the fly. This works for control types
942  * INTEGER, BOOLEAN, MENU, INTEGER MENU and BITMASK. For menu controls the
943  * @step value is interpreted as a menu_skip_mask.
944  *
945  * An error is returned if one of the range arguments is invalid for this
946  * control type.
947  *
948  * This function assumes that the control handler is not locked and will
949  * take the lock itself.
950  */
951 static inline int v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
952                                          s64 min, s64 max, u64 step, s64 def)
953 {
954         int rval;
955
956         v4l2_ctrl_lock(ctrl);
957         rval = __v4l2_ctrl_modify_range(ctrl, min, max, step, def);
958         v4l2_ctrl_unlock(ctrl);
959
960         return rval;
961 }
962
963 /**
964  * v4l2_ctrl_notify() - Function to set a notify callback for a control.
965  *
966  * @ctrl:       The control.
967  * @notify:     The callback function.
968  * @priv:       The callback private handle, passed as argument to the callback.
969  *
970  * This function sets a callback function for the control. If @ctrl is NULL,
971  * then it will do nothing. If @notify is NULL, then the notify callback will
972  * be removed.
973  *
974  * There can be only one notify. If another already exists, then a WARN_ON
975  * will be issued and the function will do nothing.
976  */
977 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify,
978                       void *priv);
979
980 /**
981  * v4l2_ctrl_get_name() - Get the name of the control
982  *
983  * @id:         The control ID.
984  *
985  * This function returns the name of the given control ID or NULL if it isn't
986  * a known control.
987  */
988 const char *v4l2_ctrl_get_name(u32 id);
989
990 /**
991  * v4l2_ctrl_get_menu() - Get the menu string array of the control
992  *
993  * @id:         The control ID.
994  *
995  * This function returns the NULL-terminated menu string array name of the
996  * given control ID or NULL if it isn't a known menu control.
997  */
998 const char * const *v4l2_ctrl_get_menu(u32 id);
999
1000 /**
1001  * v4l2_ctrl_get_int_menu() - Get the integer menu array of the control
1002  *
1003  * @id:         The control ID.
1004  * @len:        The size of the integer array.
1005  *
1006  * This function returns the integer array of the given control ID or NULL if it
1007  * if it isn't a known integer menu control.
1008  */
1009 const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len);
1010
1011 /**
1012  * v4l2_ctrl_g_ctrl() - Helper function to get the control's value from
1013  *      within a driver.
1014  *
1015  * @ctrl:       The control.
1016  *
1017  * This returns the control's value safely by going through the control
1018  * framework. This function will lock the control's handler, so it cannot be
1019  * used from within the &v4l2_ctrl_ops functions.
1020  *
1021  * This function is for integer type controls only.
1022  */
1023 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
1024
1025 /**
1026  * __v4l2_ctrl_s_ctrl() - Unlocked variant of v4l2_ctrl_s_ctrl().
1027  *
1028  * @ctrl:       The control.
1029  * @val:        The new value.
1030  *
1031  * This sets the control's new value safely by going through the control
1032  * framework. This function assumes the control's handler is already locked,
1033  * allowing it to be used from within the &v4l2_ctrl_ops functions.
1034  *
1035  * This function is for integer type controls only.
1036  */
1037 int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
1038
1039 /**
1040  * v4l2_ctrl_s_ctrl() - Helper function to set the control's value from
1041  *      within a driver.
1042  * @ctrl:       The control.
1043  * @val:        The new value.
1044  *
1045  * This sets the control's new value safely by going through the control
1046  * framework. This function will lock the control's handler, so it cannot be
1047  * used from within the &v4l2_ctrl_ops functions.
1048  *
1049  * This function is for integer type controls only.
1050  */
1051 static inline int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
1052 {
1053         int rval;
1054
1055         v4l2_ctrl_lock(ctrl);
1056         rval = __v4l2_ctrl_s_ctrl(ctrl, val);
1057         v4l2_ctrl_unlock(ctrl);
1058
1059         return rval;
1060 }
1061
1062 /**
1063  * v4l2_ctrl_g_ctrl_int64() - Helper function to get a 64-bit control's value
1064  *      from within a driver.
1065  *
1066  * @ctrl:       The control.
1067  *
1068  * This returns the control's value safely by going through the control
1069  * framework. This function will lock the control's handler, so it cannot be
1070  * used from within the &v4l2_ctrl_ops functions.
1071  *
1072  * This function is for 64-bit integer type controls only.
1073  */
1074 s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl);
1075
1076 /**
1077  * __v4l2_ctrl_s_ctrl_int64() - Unlocked variant of v4l2_ctrl_s_ctrl_int64().
1078  *
1079  * @ctrl:       The control.
1080  * @val:        The new value.
1081  *
1082  * This sets the control's new value safely by going through the control
1083  * framework. This function assumes the control's handler is already locked,
1084  * allowing it to be used from within the &v4l2_ctrl_ops functions.
1085  *
1086  * This function is for 64-bit integer type controls only.
1087  */
1088 int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val);
1089
1090 /**
1091  * v4l2_ctrl_s_ctrl_int64() - Helper function to set a 64-bit control's value
1092  *      from within a driver.
1093  *
1094  * @ctrl:       The control.
1095  * @val:        The new value.
1096  *
1097  * This sets the control's new value safely by going through the control
1098  * framework. This function will lock the control's handler, so it cannot be
1099  * used from within the &v4l2_ctrl_ops functions.
1100  *
1101  * This function is for 64-bit integer type controls only.
1102  */
1103 static inline int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
1104 {
1105         int rval;
1106
1107         v4l2_ctrl_lock(ctrl);
1108         rval = __v4l2_ctrl_s_ctrl_int64(ctrl, val);
1109         v4l2_ctrl_unlock(ctrl);
1110
1111         return rval;
1112 }
1113
1114 /**
1115  * __v4l2_ctrl_s_ctrl_string() - Unlocked variant of v4l2_ctrl_s_ctrl_string().
1116  *
1117  * @ctrl:       The control.
1118  * @s:          The new string.
1119  *
1120  * This sets the control's new string safely by going through the control
1121  * framework. This function assumes the control's handler is already locked,
1122  * allowing it to be used from within the &v4l2_ctrl_ops functions.
1123  *
1124  * This function is for string type controls only.
1125  */
1126 int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s);
1127
1128 /**
1129  * v4l2_ctrl_s_ctrl_string() - Helper function to set a control's string value
1130  *       from within a driver.
1131  *
1132  * @ctrl:       The control.
1133  * @s:          The new string.
1134  *
1135  * This sets the control's new string safely by going through the control
1136  * framework. This function will lock the control's handler, so it cannot be
1137  * used from within the &v4l2_ctrl_ops functions.
1138  *
1139  * This function is for string type controls only.
1140  */
1141 static inline int v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
1142 {
1143         int rval;
1144
1145         v4l2_ctrl_lock(ctrl);
1146         rval = __v4l2_ctrl_s_ctrl_string(ctrl, s);
1147         v4l2_ctrl_unlock(ctrl);
1148
1149         return rval;
1150 }
1151
1152 /**
1153  * __v4l2_ctrl_s_ctrl_compound() - Unlocked variant to set a compound control
1154  *
1155  * @ctrl: The control.
1156  * @type: The type of the data.
1157  * @p:    The new compound payload.
1158  *
1159  * This sets the control's new compound payload safely by going through the
1160  * control framework. This function assumes the control's handler is already
1161  * locked, allowing it to be used from within the &v4l2_ctrl_ops functions.
1162  *
1163  * This function is for compound type controls only.
1164  */
1165 int __v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
1166                                 enum v4l2_ctrl_type type, const void *p);
1167
1168 /**
1169  * v4l2_ctrl_s_ctrl_compound() - Helper function to set a compound control
1170  *      from within a driver.
1171  *
1172  * @ctrl: The control.
1173  * @type: The type of the data.
1174  * @p:    The new compound payload.
1175  *
1176  * This sets the control's new compound payload safely by going through the
1177  * control framework. This function will lock the control's handler, so it
1178  * cannot be used from within the &v4l2_ctrl_ops functions.
1179  *
1180  * This function is for compound type controls only.
1181  */
1182 static inline int v4l2_ctrl_s_ctrl_compound(struct v4l2_ctrl *ctrl,
1183                                             enum v4l2_ctrl_type type,
1184                                             const void *p)
1185 {
1186         int rval;
1187
1188         v4l2_ctrl_lock(ctrl);
1189         rval = __v4l2_ctrl_s_ctrl_compound(ctrl, type, p);
1190         v4l2_ctrl_unlock(ctrl);
1191
1192         return rval;
1193 }
1194
1195 /* Helper defines for area type controls */
1196 #define __v4l2_ctrl_s_ctrl_area(ctrl, area) \
1197         __v4l2_ctrl_s_ctrl_compound((ctrl), V4L2_CTRL_TYPE_AREA, (area))
1198 #define v4l2_ctrl_s_ctrl_area(ctrl, area) \
1199         v4l2_ctrl_s_ctrl_compound((ctrl), V4L2_CTRL_TYPE_AREA, (area))
1200
1201 /* Internal helper functions that deal with control events. */
1202 extern const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops;
1203
1204 /**
1205  * v4l2_ctrl_replace - Function to be used as a callback to
1206  *      &struct v4l2_subscribed_event_ops replace\(\)
1207  *
1208  * @old: pointer to struct &v4l2_event with the reported
1209  *       event;
1210  * @new: pointer to struct &v4l2_event with the modified
1211  *       event;
1212  */
1213 void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new);
1214
1215 /**
1216  * v4l2_ctrl_merge - Function to be used as a callback to
1217  *      &struct v4l2_subscribed_event_ops merge(\)
1218  *
1219  * @old: pointer to struct &v4l2_event with the reported
1220  *       event;
1221  * @new: pointer to struct &v4l2_event with the merged
1222  *       event;
1223  */
1224 void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new);
1225
1226 /**
1227  * v4l2_ctrl_log_status - helper function to implement %VIDIOC_LOG_STATUS ioctl
1228  *
1229  * @file: pointer to struct file
1230  * @fh: unused. Kept just to be compatible to the arguments expected by
1231  *      &struct v4l2_ioctl_ops.vidioc_log_status.
1232  *
1233  * Can be used as a vidioc_log_status function that just dumps all controls
1234  * associated with the filehandle.
1235  */
1236 int v4l2_ctrl_log_status(struct file *file, void *fh);
1237
1238 /**
1239  * v4l2_ctrl_subscribe_event - Subscribes to an event
1240  *
1241  *
1242  * @fh: pointer to struct v4l2_fh
1243  * @sub: pointer to &struct v4l2_event_subscription
1244  *
1245  * Can be used as a vidioc_subscribe_event function that just subscribes
1246  * control events.
1247  */
1248 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
1249                                 const struct v4l2_event_subscription *sub);
1250
1251 /**
1252  * v4l2_ctrl_poll - function to be used as a callback to the poll()
1253  *      That just polls for control events.
1254  *
1255  * @file: pointer to struct file
1256  * @wait: pointer to struct poll_table_struct
1257  */
1258 __poll_t v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait);
1259
1260 /**
1261  * v4l2_ctrl_request_setup - helper function to apply control values in a request
1262  *
1263  * @req: The request
1264  * @parent: The parent control handler ('priv' in media_request_object_find())
1265  *
1266  * This is a helper function to call the control handler's s_ctrl callback with
1267  * the control values contained in the request. Do note that this approach of
1268  * applying control values in a request is only applicable to memory-to-memory
1269  * devices.
1270  */
1271 int v4l2_ctrl_request_setup(struct media_request *req,
1272                              struct v4l2_ctrl_handler *parent);
1273
1274 /**
1275  * v4l2_ctrl_request_complete - Complete a control handler request object
1276  *
1277  * @req: The request
1278  * @parent: The parent control handler ('priv' in media_request_object_find())
1279  *
1280  * This function is to be called on each control handler that may have had a
1281  * request object associated with it, i.e. control handlers of a driver that
1282  * supports requests.
1283  *
1284  * The function first obtains the values of any volatile controls in the control
1285  * handler and attach them to the request. Then, the function completes the
1286  * request object.
1287  */
1288 void v4l2_ctrl_request_complete(struct media_request *req,
1289                                 struct v4l2_ctrl_handler *parent);
1290
1291 /**
1292  * v4l2_ctrl_request_hdl_find - Find the control handler in the request
1293  *
1294  * @req: The request
1295  * @parent: The parent control handler ('priv' in media_request_object_find())
1296  *
1297  * This function finds the control handler in the request. It may return
1298  * NULL if not found. When done, you must call v4l2_ctrl_request_put_hdl()
1299  * with the returned handler pointer.
1300  *
1301  * If the request is not in state VALIDATING or QUEUED, then this function
1302  * will always return NULL.
1303  *
1304  * Note that in state VALIDATING the req_queue_mutex is held, so
1305  * no objects can be added or deleted from the request.
1306  *
1307  * In state QUEUED it is the driver that will have to ensure this.
1308  */
1309 struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(struct media_request *req,
1310                                         struct v4l2_ctrl_handler *parent);
1311
1312 /**
1313  * v4l2_ctrl_request_hdl_put - Put the control handler
1314  *
1315  * @hdl: Put this control handler
1316  *
1317  * This function released the control handler previously obtained from'
1318  * v4l2_ctrl_request_hdl_find().
1319  */
1320 static inline void v4l2_ctrl_request_hdl_put(struct v4l2_ctrl_handler *hdl)
1321 {
1322         if (hdl)
1323                 media_request_object_put(&hdl->req_obj);
1324 }
1325
1326 /**
1327  * v4l2_ctrl_request_hdl_ctrl_find() - Find a control with the given ID.
1328  *
1329  * @hdl: The control handler from the request.
1330  * @id: The ID of the control to find.
1331  *
1332  * This function returns a pointer to the control if this control is
1333  * part of the request or NULL otherwise.
1334  */
1335 struct v4l2_ctrl *
1336 v4l2_ctrl_request_hdl_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
1337
1338 /* Helpers for ioctl_ops */
1339
1340 /**
1341  * v4l2_queryctrl - Helper function to implement
1342  *      :ref:`VIDIOC_QUERYCTRL <vidioc_queryctrl>` ioctl
1343  *
1344  * @hdl: pointer to &struct v4l2_ctrl_handler
1345  * @qc: pointer to &struct v4l2_queryctrl
1346  *
1347  * If hdl == NULL then they will all return -EINVAL.
1348  */
1349 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
1350
1351 /**
1352  * v4l2_query_ext_ctrl - Helper function to implement
1353  *       :ref:`VIDIOC_QUERY_EXT_CTRL <vidioc_queryctrl>` ioctl
1354  *
1355  * @hdl: pointer to &struct v4l2_ctrl_handler
1356  * @qc: pointer to &struct v4l2_query_ext_ctrl
1357  *
1358  * If hdl == NULL then they will all return -EINVAL.
1359  */
1360 int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl,
1361                         struct v4l2_query_ext_ctrl *qc);
1362
1363 /**
1364  * v4l2_querymenu - Helper function to implement
1365  *      :ref:`VIDIOC_QUERYMENU <vidioc_queryctrl>` ioctl
1366  *
1367  * @hdl: pointer to &struct v4l2_ctrl_handler
1368  * @qm: pointer to &struct v4l2_querymenu
1369  *
1370  * If hdl == NULL then they will all return -EINVAL.
1371  */
1372 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
1373
1374 /**
1375  * v4l2_g_ctrl - Helper function to implement
1376  *      :ref:`VIDIOC_G_CTRL <vidioc_g_ctrl>` ioctl
1377  *
1378  * @hdl: pointer to &struct v4l2_ctrl_handler
1379  * @ctrl: pointer to &struct v4l2_control
1380  *
1381  * If hdl == NULL then they will all return -EINVAL.
1382  */
1383 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
1384
1385 /**
1386  * v4l2_s_ctrl - Helper function to implement
1387  *      :ref:`VIDIOC_S_CTRL <vidioc_g_ctrl>` ioctl
1388  *
1389  * @fh: pointer to &struct v4l2_fh
1390  * @hdl: pointer to &struct v4l2_ctrl_handler
1391  *
1392  * @ctrl: pointer to &struct v4l2_control
1393  *
1394  * If hdl == NULL then they will all return -EINVAL.
1395  */
1396 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1397                 struct v4l2_control *ctrl);
1398
1399 /**
1400  * v4l2_g_ext_ctrls - Helper function to implement
1401  *      :ref:`VIDIOC_G_EXT_CTRLS <vidioc_g_ext_ctrls>` ioctl
1402  *
1403  * @hdl: pointer to &struct v4l2_ctrl_handler
1404  * @vdev: pointer to &struct video_device
1405  * @mdev: pointer to &struct media_device
1406  * @c: pointer to &struct v4l2_ext_controls
1407  *
1408  * If hdl == NULL then they will all return -EINVAL.
1409  */
1410 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct video_device *vdev,
1411                      struct media_device *mdev, struct v4l2_ext_controls *c);
1412
1413 /**
1414  * v4l2_try_ext_ctrls - Helper function to implement
1415  *      :ref:`VIDIOC_TRY_EXT_CTRLS <vidioc_g_ext_ctrls>` ioctl
1416  *
1417  * @hdl: pointer to &struct v4l2_ctrl_handler
1418  * @vdev: pointer to &struct video_device
1419  * @mdev: pointer to &struct media_device
1420  * @c: pointer to &struct v4l2_ext_controls
1421  *
1422  * If hdl == NULL then they will all return -EINVAL.
1423  */
1424 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl,
1425                        struct video_device *vdev,
1426                        struct media_device *mdev,
1427                        struct v4l2_ext_controls *c);
1428
1429 /**
1430  * v4l2_s_ext_ctrls - Helper function to implement
1431  *      :ref:`VIDIOC_S_EXT_CTRLS <vidioc_g_ext_ctrls>` ioctl
1432  *
1433  * @fh: pointer to &struct v4l2_fh
1434  * @hdl: pointer to &struct v4l2_ctrl_handler
1435  * @vdev: pointer to &struct video_device
1436  * @mdev: pointer to &struct media_device
1437  * @c: pointer to &struct v4l2_ext_controls
1438  *
1439  * If hdl == NULL then they will all return -EINVAL.
1440  */
1441 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
1442                      struct video_device *vdev,
1443                      struct media_device *mdev,
1444                      struct v4l2_ext_controls *c);
1445
1446 /**
1447  * v4l2_ctrl_subdev_subscribe_event - Helper function to implement
1448  *      as a &struct v4l2_subdev_core_ops subscribe_event function
1449  *      that just subscribes control events.
1450  *
1451  * @sd: pointer to &struct v4l2_subdev
1452  * @fh: pointer to &struct v4l2_fh
1453  * @sub: pointer to &struct v4l2_event_subscription
1454  */
1455 int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1456                                      struct v4l2_event_subscription *sub);
1457
1458 /**
1459  * v4l2_ctrl_subdev_log_status - Log all controls owned by subdev's control
1460  *       handler.
1461  *
1462  * @sd: pointer to &struct v4l2_subdev
1463  */
1464 int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd);
1465
1466 /**
1467  * v4l2_ctrl_new_fwnode_properties() - Register controls for the device
1468  *                                     properties
1469  *
1470  * @hdl: pointer to &struct v4l2_ctrl_handler to register controls on
1471  * @ctrl_ops: pointer to &struct v4l2_ctrl_ops to register controls with
1472  * @p: pointer to &struct v4l2_fwnode_device_properties
1473  *
1474  * This function registers controls associated to device properties, using the
1475  * property values contained in @p parameter, if the property has been set to
1476  * a value.
1477  *
1478  * Currently the following v4l2 controls are parsed and registered:
1479  * - V4L2_CID_CAMERA_ORIENTATION
1480  * - V4L2_CID_CAMERA_SENSOR_ROTATION;
1481  *
1482  * Controls already registered by the caller with the @hdl control handler are
1483  * not overwritten. Callers should register the controls they want to handle
1484  * themselves before calling this function.
1485  *
1486  * Return: 0 on success, a negative error code on failure.
1487  */
1488 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl,
1489                                     const struct v4l2_ctrl_ops *ctrl_ops,
1490                                     const struct v4l2_fwnode_device_properties *p);
1491 #endif