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