]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/media/v4l2-ctrls.h
device.h: audit and cleanup users in main include dir
[mirror_ubuntu-hirsute-kernel.git] / include / media / v4l2-ctrls.h
CommitLineData
0996517c
HV
1/*
2 V4L2 controls support header.
3
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef _V4L2_CTRLS_H
22#define _V4L2_CTRLS_H
23
24#include <linux/list.h>
01c40c04 25#include <linux/videodev2.h>
0996517c
HV
26
27/* forward references */
28struct v4l2_ctrl_handler;
eb5b16ef 29struct v4l2_ctrl_helper;
0996517c
HV
30struct v4l2_ctrl;
31struct video_device;
32struct v4l2_subdev;
77068d36 33struct v4l2_subscribed_event;
6e239399 34struct v4l2_fh;
0996517c
HV
35
36/** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
37 * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
38 * for volatile (and usually read-only) controls such as a control
39 * that returns the current signal strength which changes
40 * continuously.
41 * If not set, then the currently cached value will be returned.
42 * @try_ctrl: Test whether the control's value is valid. Only relevant when
43 * the usual min/max/step checks are not sufficient.
44 * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
45 * ctrl->handler->lock is held when these ops are called, so no
46 * one else can access controls owned by that handler.
47 */
48struct v4l2_ctrl_ops {
49 int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
50 int (*try_ctrl)(struct v4l2_ctrl *ctrl);
51 int (*s_ctrl)(struct v4l2_ctrl *ctrl);
52};
53
54/** struct v4l2_ctrl - The control structure.
55 * @node: The list node.
77068d36 56 * @ev_subs: The list of control event subscriptions.
0996517c
HV
57 * @handler: The handler that owns the control.
58 * @cluster: Point to start of cluster array.
59 * @ncontrols: Number of controls in cluster array.
0996517c 60 * @done: Internal flag: set for each processed control.
2a863793
HV
61 * @is_new: Set when the user specified a new value for this control. It
62 * is also set when called from v4l2_ctrl_handler_setup. Drivers
63 * should never set this flag.
0996517c
HV
64 * @is_private: If set, then this control is private to its handler and it
65 * will not be added to any other handlers. Drivers can set
66 * this flag.
72d877ca
HV
67 * @is_auto: If set, then this control selects whether the other cluster
68 * members are in 'automatic' mode or 'manual' mode. This is
69 * used for autogain/gain type clusters. Drivers should never
70 * set this flag directly.
5626b8c7
HV
71 * @has_volatiles: If set, then one or more members of the cluster are volatile.
72 * Drivers should never touch this flag.
72d877ca
HV
73 * @manual_mode_value: If the is_auto flag is set, then this is the value
74 * of the auto control that determines if that control is in
75 * manual mode. So if the value of the auto control equals this
76 * value, then the whole cluster is in manual mode. Drivers should
77 * never set this flag directly.
0996517c
HV
78 * @ops: The control ops.
79 * @id: The control ID.
80 * @name: The control name.
81 * @type: The control type.
82 * @minimum: The control's minimum value.
83 * @maximum: The control's maximum value.
84 * @default_value: The control's default value.
85 * @step: The control's step value for non-menu controls.
86 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
87 * easy to skip menu items that are not valid. If bit X is set,
88 * then menu item X is skipped. Of course, this only works for
89 * menus with <= 32 menu items. There are no menus that come
90 * close to that number, so this is OK. Should we ever need more,
91 * then this will have to be extended to a u64 or a bit array.
92 * @qmenu: A const char * array for all menu items. Array entries that are
93 * empty strings ("") correspond to non-existing menu items (this
94 * is in addition to the menu_skip_mask above). The last entry
95 * must be NULL.
96 * @flags: The control's flags.
97 * @cur: The control's current value.
98 * @val: The control's new s32 value.
99 * @val64: The control's new s64 value.
100 * @string: The control's new string value.
101 * @priv: The control's private pointer. For use by the driver. It is
102 * untouched by the control framework. Note that this pointer is
103 * not freed when the control is deleted. Should this be needed
104 * then a new internal bitfield can be added to tell the framework
105 * to free this pointer.
106 */
107struct v4l2_ctrl {
108 /* Administrative fields */
109 struct list_head node;
77068d36 110 struct list_head ev_subs;
0996517c
HV
111 struct v4l2_ctrl_handler *handler;
112 struct v4l2_ctrl **cluster;
113 unsigned ncontrols;
0996517c
HV
114 unsigned int done:1;
115
2a863793 116 unsigned int is_new:1;
0996517c 117 unsigned int is_private:1;
72d877ca 118 unsigned int is_auto:1;
5626b8c7 119 unsigned int has_volatiles:1;
82a7c049 120 unsigned int manual_mode_value:8;
0996517c
HV
121
122 const struct v4l2_ctrl_ops *ops;
123 u32 id;
124 const char *name;
125 enum v4l2_ctrl_type type;
126 s32 minimum, maximum, default_value;
127 union {
128 u32 step;
129 u32 menu_skip_mask;
130 };
513521ea 131 const char * const *qmenu;
0996517c
HV
132 unsigned long flags;
133 union {
134 s32 val;
135 s64 val64;
136 char *string;
137 } cur;
138 union {
139 s32 val;
140 s64 val64;
141 char *string;
142 };
143 void *priv;
144};
145
146/** struct v4l2_ctrl_ref - The control reference.
147 * @node: List node for the sorted list.
148 * @next: Single-link list node for the hash.
149 * @ctrl: The actual control information.
eb5b16ef 150 * @helper: Pointer to helper struct. Used internally in prepare_ext_ctrls().
0996517c
HV
151 *
152 * Each control handler has a list of these refs. The list_head is used to
153 * keep a sorted-by-control-ID list of all controls, while the next pointer
154 * is used to link the control in the hash's bucket.
155 */
156struct v4l2_ctrl_ref {
157 struct list_head node;
158 struct v4l2_ctrl_ref *next;
159 struct v4l2_ctrl *ctrl;
eb5b16ef 160 struct v4l2_ctrl_helper *helper;
0996517c
HV
161};
162
163/** struct v4l2_ctrl_handler - The control handler keeps track of all the
164 * controls: both the controls owned by the handler and those inherited
165 * from other handlers.
166 * @lock: Lock to control access to this handler and its controls.
167 * @ctrls: The list of controls owned by this handler.
168 * @ctrl_refs: The list of control references.
169 * @cached: The last found control reference. It is common that the same
170 * control is needed multiple times, so this is a simple
171 * optimization.
172 * @buckets: Buckets for the hashing. Allows for quick control lookup.
173 * @nr_of_buckets: Total number of buckets in the array.
174 * @error: The error code of the first failed control addition.
175 */
176struct v4l2_ctrl_handler {
177 struct mutex lock;
178 struct list_head ctrls;
179 struct list_head ctrl_refs;
180 struct v4l2_ctrl_ref *cached;
181 struct v4l2_ctrl_ref **buckets;
182 u16 nr_of_buckets;
183 int error;
184};
185
186/** struct v4l2_ctrl_config - Control configuration structure.
187 * @ops: The control ops.
188 * @id: The control ID.
189 * @name: The control name.
190 * @type: The control type.
191 * @min: The control's minimum value.
192 * @max: The control's maximum value.
193 * @step: The control's step value for non-menu controls.
194 * @def: The control's default value.
195 * @flags: The control's flags.
196 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
197 * easy to skip menu items that are not valid. If bit X is set,
198 * then menu item X is skipped. Of course, this only works for
199 * menus with <= 32 menu items. There are no menus that come
200 * close to that number, so this is OK. Should we ever need more,
201 * then this will have to be extended to a u64 or a bit array.
202 * @qmenu: A const char * array for all menu items. Array entries that are
203 * empty strings ("") correspond to non-existing menu items (this
204 * is in addition to the menu_skip_mask above). The last entry
205 * must be NULL.
206 * @is_private: If set, then this control is private to its handler and it
207 * will not be added to any other handlers.
0996517c
HV
208 */
209struct v4l2_ctrl_config {
210 const struct v4l2_ctrl_ops *ops;
211 u32 id;
212 const char *name;
213 enum v4l2_ctrl_type type;
214 s32 min;
215 s32 max;
216 u32 step;
217 s32 def;
218 u32 flags;
219 u32 menu_skip_mask;
513521ea 220 const char * const *qmenu;
0996517c 221 unsigned int is_private:1;
0996517c
HV
222};
223
224/** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
225 *
226 * This works for all standard V4L2 controls.
227 * For non-standard controls it will only fill in the given arguments
228 * and @name will be NULL.
229 *
230 * This function will overwrite the contents of @name, @type and @flags.
231 * The contents of @min, @max, @step and @def may be modified depending on
232 * the type.
233 *
234 * Do not use in drivers! It is used internally for backwards compatibility
235 * control handling only. Once all drivers are converted to use the new
236 * control framework this function will no longer be exported.
237 */
238void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
239 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags);
240
241
242/** v4l2_ctrl_handler_init() - Initialize the control handler.
243 * @hdl: The control handler.
244 * @nr_of_controls_hint: A hint of how many controls this handler is
245 * expected to refer to. This is the total number, so including
246 * any inherited controls. It doesn't have to be precise, but if
247 * it is way off, then you either waste memory (too many buckets
248 * are allocated) or the control lookup becomes slower (not enough
249 * buckets are allocated, so there are more slow list lookups).
250 * It will always work, though.
251 *
252 * Returns an error if the buckets could not be allocated. This error will
253 * also be stored in @hdl->error.
254 */
255int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
256 unsigned nr_of_controls_hint);
257
258/** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
259 * the control list.
260 * @hdl: The control handler.
261 *
262 * Does nothing if @hdl == NULL.
263 */
264void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
265
266/** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
267 * to the handler to initialize the hardware to the current control values.
268 * @hdl: The control handler.
269 *
270 * Button controls will be skipped, as are read-only controls.
271 *
272 * If @hdl == NULL, then this just returns 0.
273 */
274int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
275
276/** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
277 * @hdl: The control handler.
278 * @prefix: The prefix to use when logging the control values. If the
279 * prefix does not end with a space, then ": " will be added
280 * after the prefix. If @prefix == NULL, then no prefix will be
281 * used.
282 *
283 * For use with VIDIOC_LOG_STATUS.
284 *
285 * Does nothing if @hdl == NULL.
286 */
287void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
288 const char *prefix);
289
290/** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
291 * control.
292 * @hdl: The control handler.
293 * @cfg: The control's configuration data.
294 * @priv: The control's driver-specific private data.
295 *
296 * If the &v4l2_ctrl struct could not be allocated then NULL is returned
297 * and @hdl->error is set to the error code (if it wasn't set already).
298 */
299struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
300 const struct v4l2_ctrl_config *cfg, void *priv);
301
302/** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
303 * @hdl: The control handler.
304 * @ops: The control ops.
305 * @id: The control ID.
306 * @min: The control's minimum value.
307 * @max: The control's maximum value.
308 * @step: The control's step value
309 * @def: The control's default value.
310 *
311 * If the &v4l2_ctrl struct could not be allocated, or the control
312 * ID is not known, then NULL is returned and @hdl->error is set to the
313 * appropriate error code (if it wasn't set already).
314 *
315 * If @id refers to a menu control, then this function will return NULL.
316 *
317 * Use v4l2_ctrl_new_std_menu() when adding menu controls.
318 */
319struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
320 const struct v4l2_ctrl_ops *ops,
321 u32 id, s32 min, s32 max, u32 step, s32 def);
322
323/** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
324 * @hdl: The control handler.
325 * @ops: The control ops.
326 * @id: The control ID.
327 * @max: The control's maximum value.
328 * @mask: The control's skip mask for menu controls. This makes it
329 * easy to skip menu items that are not valid. If bit X is set,
330 * then menu item X is skipped. Of course, this only works for
331 * menus with <= 32 menu items. There are no menus that come
332 * close to that number, so this is OK. Should we ever need more,
333 * then this will have to be extended to a u64 or a bit array.
334 * @def: The control's default value.
335 *
336 * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
337 * determines which menu items are to be skipped.
338 *
339 * If @id refers to a non-menu control, then this function will return NULL.
340 */
341struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
342 const struct v4l2_ctrl_ops *ops,
343 u32 id, s32 max, s32 mask, s32 def);
344
345/** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
346 * @hdl: The control handler.
347 * @ctrl: The control to add.
348 *
349 * It will return NULL if it was unable to add the control reference.
350 * If the control already belonged to the handler, then it will do
351 * nothing and just return @ctrl.
352 */
353struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
354 struct v4l2_ctrl *ctrl);
355
356/** v4l2_ctrl_add_handler() - Add all controls from handler @add to
357 * handler @hdl.
358 * @hdl: The control handler.
359 * @add: The control handler whose controls you want to add to
360 * the @hdl control handler.
361 *
362 * Does nothing if either of the two is a NULL pointer.
363 * In case of an error @hdl->error will be set to the error code (if it
364 * wasn't set already).
365 */
366int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
367 struct v4l2_ctrl_handler *add);
368
369
370/** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
371 * @ncontrols: The number of controls in this cluster.
372 * @controls: The cluster control array of size @ncontrols.
373 */
374void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
375
376
72d877ca
HV
377/** v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
378 * that cluster and set it up for autofoo/foo-type handling.
379 * @ncontrols: The number of controls in this cluster.
380 * @controls: The cluster control array of size @ncontrols. The first control
381 * must be the 'auto' control (e.g. autogain, autoexposure, etc.)
382 * @manual_val: The value for the first control in the cluster that equals the
383 * manual setting.
384 * @set_volatile: If true, then all controls except the first auto control will
88365105 385 * be volatile.
72d877ca
HV
386 *
387 * Use for control groups where one control selects some automatic feature and
388 * the other controls are only active whenever the automatic feature is turned
389 * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
390 * red and blue balance, etc.
391 *
392 * The behavior of such controls is as follows:
393 *
394 * When the autofoo control is set to automatic, then any manual controls
395 * are set to inactive and any reads will call g_volatile_ctrl (if the control
396 * was marked volatile).
397 *
398 * When the autofoo control is set to manual, then any manual controls will
399 * be marked active, and any reads will just return the current value without
400 * going through g_volatile_ctrl.
401 *
402 * In addition, this function will set the V4L2_CTRL_FLAG_UPDATE flag
403 * on the autofoo control and V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
404 * if autofoo is in auto mode.
405 */
406void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
407 u8 manual_val, bool set_volatile);
408
409
0996517c
HV
410/** v4l2_ctrl_find() - Find a control with the given ID.
411 * @hdl: The control handler.
412 * @id: The control ID to find.
413 *
414 * If @hdl == NULL this will return NULL as well. Will lock the handler so
415 * do not use from inside &v4l2_ctrl_ops.
416 */
417struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
418
419/** v4l2_ctrl_activate() - Make the control active or inactive.
420 * @ctrl: The control to (de)activate.
421 * @active: True if the control should become active.
422 *
423 * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
424 * Does nothing if @ctrl == NULL.
425 * This will usually be called from within the s_ctrl op.
6e239399 426 * The V4L2_EVENT_CTRL event will be generated afterwards.
0996517c 427 *
6e239399 428 * This function assumes that the control handler is locked.
0996517c
HV
429 */
430void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
431
432/** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
433 * @ctrl: The control to (de)activate.
434 * @grabbed: True if the control should become grabbed.
435 *
436 * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
437 * Does nothing if @ctrl == NULL.
6e239399 438 * The V4L2_EVENT_CTRL event will be generated afterwards.
0996517c
HV
439 * This will usually be called when starting or stopping streaming in the
440 * driver.
441 *
6e239399
HV
442 * This function assumes that the control handler is not locked and will
443 * take the lock itself.
0996517c
HV
444 */
445void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
446
447/** v4l2_ctrl_lock() - Helper function to lock the handler
448 * associated with the control.
449 * @ctrl: The control to lock.
450 */
451static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
452{
453 mutex_lock(&ctrl->handler->lock);
454}
455
456/** v4l2_ctrl_lock() - Helper function to unlock the handler
457 * associated with the control.
458 * @ctrl: The control to unlock.
459 */
460static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
461{
462 mutex_unlock(&ctrl->handler->lock);
463}
464
465/** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
466 * @ctrl: The control.
467 *
468 * This returns the control's value safely by going through the control
469 * framework. This function will lock the control's handler, so it cannot be
470 * used from within the &v4l2_ctrl_ops functions.
471 *
472 * This function is for integer type controls only.
473 */
474s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
475
476/** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
477 * @ctrl: The control.
478 * @val: The new value.
479 *
480 * This set the control's new value safely by going through the control
481 * framework. This function will lock the control's handler, so it cannot be
482 * used from within the &v4l2_ctrl_ops functions.
483 *
484 * This function is for integer type controls only.
485 */
486int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
487
ce727574 488/* Internal helper functions that deal with control events. */
77068d36
HV
489void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
490 struct v4l2_subscribed_event *sev);
491void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
492 struct v4l2_subscribed_event *sev);
6e239399 493
0996517c
HV
494/* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
495int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
496int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
497int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
ab892bac
HV
498int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
499 struct v4l2_control *ctrl);
0996517c
HV
500int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
501int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
ab892bac
HV
502int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
503 struct v4l2_ext_controls *c);
0996517c
HV
504
505/* Helpers for subdevices. If the associated ctrl_handler == NULL then they
506 will all return -EINVAL. */
507int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
508int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
509int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
510int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
511int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
512int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
513int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
514
515#endif