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