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