blob: fcc9a0cf8ff1a75a0d416ebd960206598731b9f4 [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>
26
27/* forward references */
28struct v4l2_ctrl_handler;
29struct v4l2_ctrl;
30struct video_device;
31struct v4l2_subdev;
32
33/** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
34 * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
35 * for volatile (and usually read-only) controls such as a control
36 * that returns the current signal strength which changes
37 * continuously.
38 * If not set, then the currently cached value will be returned.
39 * @try_ctrl: Test whether the control's value is valid. Only relevant when
40 * the usual min/max/step checks are not sufficient.
41 * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
42 * ctrl->handler->lock is held when these ops are called, so no
43 * one else can access controls owned by that handler.
44 */
45struct v4l2_ctrl_ops {
46 int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
47 int (*try_ctrl)(struct v4l2_ctrl *ctrl);
48 int (*s_ctrl)(struct v4l2_ctrl *ctrl);
49};
50
51/** struct v4l2_ctrl - The control structure.
52 * @node: The list node.
53 * @handler: The handler that owns the control.
54 * @cluster: Point to start of cluster array.
55 * @ncontrols: Number of controls in cluster array.
Hans Verkuil09965172010-08-01 14:32:42 -030056 * @done: Internal flag: set for each processed control.
Hans Verkuil2a863792011-01-11 14:45:03 -030057 * @is_new: Set when the user specified a new value for this control. It
58 * is also set when called from v4l2_ctrl_handler_setup. Drivers
59 * should never set this flag.
Hans Verkuil09965172010-08-01 14:32:42 -030060 * @is_private: If set, then this control is private to its handler and it
61 * will not be added to any other handlers. Drivers can set
62 * this flag.
63 * @is_volatile: If set, then this control is volatile. This means that the
64 * control's current value cannot be cached and needs to be
65 * retrieved through the g_volatile_ctrl op. Drivers can set
66 * this flag.
67 * @ops: The control ops.
68 * @id: The control ID.
69 * @name: The control name.
70 * @type: The control type.
71 * @minimum: The control's minimum value.
72 * @maximum: The control's maximum value.
73 * @default_value: The control's default value.
74 * @step: The control's step value for non-menu controls.
75 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
76 * easy to skip menu items that are not valid. If bit X is set,
77 * then menu item X is skipped. Of course, this only works for
78 * menus with <= 32 menu items. There are no menus that come
79 * close to that number, so this is OK. Should we ever need more,
80 * then this will have to be extended to a u64 or a bit array.
81 * @qmenu: A const char * array for all menu items. Array entries that are
82 * empty strings ("") correspond to non-existing menu items (this
83 * is in addition to the menu_skip_mask above). The last entry
84 * must be NULL.
85 * @flags: The control's flags.
86 * @cur: The control's current value.
87 * @val: The control's new s32 value.
88 * @val64: The control's new s64 value.
89 * @string: The control's new string value.
90 * @priv: The control's private pointer. For use by the driver. It is
91 * untouched by the control framework. Note that this pointer is
92 * not freed when the control is deleted. Should this be needed
93 * then a new internal bitfield can be added to tell the framework
94 * to free this pointer.
95 */
96struct v4l2_ctrl {
97 /* Administrative fields */
98 struct list_head node;
99 struct v4l2_ctrl_handler *handler;
100 struct v4l2_ctrl **cluster;
101 unsigned ncontrols;
Hans Verkuil09965172010-08-01 14:32:42 -0300102 unsigned int done:1;
103
Hans Verkuil2a863792011-01-11 14:45:03 -0300104 unsigned int is_new:1;
Hans Verkuil09965172010-08-01 14:32:42 -0300105 unsigned int is_private:1;
106 unsigned int is_volatile:1;
107
108 const struct v4l2_ctrl_ops *ops;
109 u32 id;
110 const char *name;
111 enum v4l2_ctrl_type type;
112 s32 minimum, maximum, default_value;
113 union {
114 u32 step;
115 u32 menu_skip_mask;
116 };
Hans Verkuil513521e2010-12-29 14:25:52 -0300117 const char * const *qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -0300118 unsigned long flags;
119 union {
120 s32 val;
121 s64 val64;
122 char *string;
123 } cur;
124 union {
125 s32 val;
126 s64 val64;
127 char *string;
128 };
129 void *priv;
130};
131
132/** struct v4l2_ctrl_ref - The control reference.
133 * @node: List node for the sorted list.
134 * @next: Single-link list node for the hash.
135 * @ctrl: The actual control information.
136 *
137 * Each control handler has a list of these refs. The list_head is used to
138 * keep a sorted-by-control-ID list of all controls, while the next pointer
139 * is used to link the control in the hash's bucket.
140 */
141struct v4l2_ctrl_ref {
142 struct list_head node;
143 struct v4l2_ctrl_ref *next;
144 struct v4l2_ctrl *ctrl;
145};
146
147/** struct v4l2_ctrl_handler - The control handler keeps track of all the
148 * controls: both the controls owned by the handler and those inherited
149 * from other handlers.
150 * @lock: Lock to control access to this handler and its controls.
151 * @ctrls: The list of controls owned by this handler.
152 * @ctrl_refs: The list of control references.
153 * @cached: The last found control reference. It is common that the same
154 * control is needed multiple times, so this is a simple
155 * optimization.
156 * @buckets: Buckets for the hashing. Allows for quick control lookup.
157 * @nr_of_buckets: Total number of buckets in the array.
158 * @error: The error code of the first failed control addition.
159 */
160struct v4l2_ctrl_handler {
161 struct mutex lock;
162 struct list_head ctrls;
163 struct list_head ctrl_refs;
164 struct v4l2_ctrl_ref *cached;
165 struct v4l2_ctrl_ref **buckets;
166 u16 nr_of_buckets;
167 int error;
168};
169
170/** struct v4l2_ctrl_config - Control configuration structure.
171 * @ops: The control ops.
172 * @id: The control ID.
173 * @name: The control name.
174 * @type: The control type.
175 * @min: The control's minimum value.
176 * @max: The control's maximum value.
177 * @step: The control's step value for non-menu controls.
178 * @def: The control's default value.
179 * @flags: The control's flags.
180 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
181 * easy to skip menu items that are not valid. If bit X is set,
182 * then menu item X is skipped. Of course, this only works for
183 * menus with <= 32 menu items. There are no menus that come
184 * close to that number, so this is OK. Should we ever need more,
185 * then this will have to be extended to a u64 or a bit array.
186 * @qmenu: A const char * array for all menu items. Array entries that are
187 * empty strings ("") correspond to non-existing menu items (this
188 * is in addition to the menu_skip_mask above). The last entry
189 * must be NULL.
190 * @is_private: If set, then this control is private to its handler and it
191 * will not be added to any other handlers.
192 * @is_volatile: If set, then this control is volatile. This means that the
193 * control's current value cannot be cached and needs to be
194 * retrieved through the g_volatile_ctrl op.
195 */
196struct v4l2_ctrl_config {
197 const struct v4l2_ctrl_ops *ops;
198 u32 id;
199 const char *name;
200 enum v4l2_ctrl_type type;
201 s32 min;
202 s32 max;
203 u32 step;
204 s32 def;
205 u32 flags;
206 u32 menu_skip_mask;
Hans Verkuil513521e2010-12-29 14:25:52 -0300207 const char * const *qmenu;
Hans Verkuil09965172010-08-01 14:32:42 -0300208 unsigned int is_private:1;
209 unsigned int is_volatile:1;
210};
211
212/** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
213 *
214 * This works for all standard V4L2 controls.
215 * For non-standard controls it will only fill in the given arguments
216 * and @name will be NULL.
217 *
218 * This function will overwrite the contents of @name, @type and @flags.
219 * The contents of @min, @max, @step and @def may be modified depending on
220 * the type.
221 *
222 * Do not use in drivers! It is used internally for backwards compatibility
223 * control handling only. Once all drivers are converted to use the new
224 * control framework this function will no longer be exported.
225 */
226void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
227 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags);
228
229
230/** v4l2_ctrl_handler_init() - Initialize the control handler.
231 * @hdl: The control handler.
232 * @nr_of_controls_hint: A hint of how many controls this handler is
233 * expected to refer to. This is the total number, so including
234 * any inherited controls. It doesn't have to be precise, but if
235 * it is way off, then you either waste memory (too many buckets
236 * are allocated) or the control lookup becomes slower (not enough
237 * buckets are allocated, so there are more slow list lookups).
238 * It will always work, though.
239 *
240 * Returns an error if the buckets could not be allocated. This error will
241 * also be stored in @hdl->error.
242 */
243int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
244 unsigned nr_of_controls_hint);
245
246/** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
247 * the control list.
248 * @hdl: The control handler.
249 *
250 * Does nothing if @hdl == NULL.
251 */
252void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
253
254/** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
255 * to the handler to initialize the hardware to the current control values.
256 * @hdl: The control handler.
257 *
258 * Button controls will be skipped, as are read-only controls.
259 *
260 * If @hdl == NULL, then this just returns 0.
261 */
262int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
263
264/** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
265 * @hdl: The control handler.
266 * @prefix: The prefix to use when logging the control values. If the
267 * prefix does not end with a space, then ": " will be added
268 * after the prefix. If @prefix == NULL, then no prefix will be
269 * used.
270 *
271 * For use with VIDIOC_LOG_STATUS.
272 *
273 * Does nothing if @hdl == NULL.
274 */
275void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
276 const char *prefix);
277
278/** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
279 * control.
280 * @hdl: The control handler.
281 * @cfg: The control's configuration data.
282 * @priv: The control's driver-specific private data.
283 *
284 * If the &v4l2_ctrl struct could not be allocated then NULL is returned
285 * and @hdl->error is set to the error code (if it wasn't set already).
286 */
287struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
288 const struct v4l2_ctrl_config *cfg, void *priv);
289
290/** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
291 * @hdl: The control handler.
292 * @ops: The control ops.
293 * @id: The control ID.
294 * @min: The control's minimum value.
295 * @max: The control's maximum value.
296 * @step: The control's step value
297 * @def: The control's default value.
298 *
299 * If the &v4l2_ctrl struct could not be allocated, or the control
300 * ID is not known, then NULL is returned and @hdl->error is set to the
301 * appropriate error code (if it wasn't set already).
302 *
303 * If @id refers to a menu control, then this function will return NULL.
304 *
305 * Use v4l2_ctrl_new_std_menu() when adding menu controls.
306 */
307struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
308 const struct v4l2_ctrl_ops *ops,
309 u32 id, s32 min, s32 max, u32 step, s32 def);
310
311/** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
312 * @hdl: The control handler.
313 * @ops: The control ops.
314 * @id: The control ID.
315 * @max: The control's maximum value.
316 * @mask: The control's skip mask for menu controls. This makes it
317 * easy to skip menu items that are not valid. If bit X is set,
318 * then menu item X is skipped. Of course, this only works for
319 * menus with <= 32 menu items. There are no menus that come
320 * close to that number, so this is OK. Should we ever need more,
321 * then this will have to be extended to a u64 or a bit array.
322 * @def: The control's default value.
323 *
324 * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
325 * determines which menu items are to be skipped.
326 *
327 * If @id refers to a non-menu control, then this function will return NULL.
328 */
329struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
330 const struct v4l2_ctrl_ops *ops,
331 u32 id, s32 max, s32 mask, s32 def);
332
333/** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
334 * @hdl: The control handler.
335 * @ctrl: The control to add.
336 *
337 * It will return NULL if it was unable to add the control reference.
338 * If the control already belonged to the handler, then it will do
339 * nothing and just return @ctrl.
340 */
341struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
342 struct v4l2_ctrl *ctrl);
343
344/** v4l2_ctrl_add_handler() - Add all controls from handler @add to
345 * handler @hdl.
346 * @hdl: The control handler.
347 * @add: The control handler whose controls you want to add to
348 * the @hdl control handler.
349 *
350 * Does nothing if either of the two is a NULL pointer.
351 * In case of an error @hdl->error will be set to the error code (if it
352 * wasn't set already).
353 */
354int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
355 struct v4l2_ctrl_handler *add);
356
357
358/** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
359 * @ncontrols: The number of controls in this cluster.
360 * @controls: The cluster control array of size @ncontrols.
361 */
362void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
363
364
365/** v4l2_ctrl_find() - Find a control with the given ID.
366 * @hdl: The control handler.
367 * @id: The control ID to find.
368 *
369 * If @hdl == NULL this will return NULL as well. Will lock the handler so
370 * do not use from inside &v4l2_ctrl_ops.
371 */
372struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
373
374/** v4l2_ctrl_activate() - Make the control active or inactive.
375 * @ctrl: The control to (de)activate.
376 * @active: True if the control should become active.
377 *
378 * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
379 * Does nothing if @ctrl == NULL.
380 * This will usually be called from within the s_ctrl op.
381 *
382 * This function can be called regardless of whether the control handler
383 * is locked or not.
384 */
385void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
386
387/** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
388 * @ctrl: The control to (de)activate.
389 * @grabbed: True if the control should become grabbed.
390 *
391 * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
392 * Does nothing if @ctrl == NULL.
393 * This will usually be called when starting or stopping streaming in the
394 * driver.
395 *
396 * This function can be called regardless of whether the control handler
397 * is locked or not.
398 */
399void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
400
401/** v4l2_ctrl_lock() - Helper function to lock the handler
402 * associated with the control.
403 * @ctrl: The control to lock.
404 */
405static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
406{
407 mutex_lock(&ctrl->handler->lock);
408}
409
410/** v4l2_ctrl_lock() - Helper function to unlock the handler
411 * associated with the control.
412 * @ctrl: The control to unlock.
413 */
414static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
415{
416 mutex_unlock(&ctrl->handler->lock);
417}
418
419/** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
420 * @ctrl: The control.
421 *
422 * This returns the control's value safely by going through the control
423 * framework. This function will lock the control's handler, so it cannot be
424 * used from within the &v4l2_ctrl_ops functions.
425 *
426 * This function is for integer type controls only.
427 */
428s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
429
430/** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
431 * @ctrl: The control.
432 * @val: The new value.
433 *
434 * This set the control's new value safely by going through the control
435 * framework. This function will lock the control's handler, so it cannot be
436 * used from within the &v4l2_ctrl_ops functions.
437 *
438 * This function is for integer type controls only.
439 */
440int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
441
442
443/* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
444int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
445int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
446int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
447int v4l2_s_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
448int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
449int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
450int v4l2_s_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
451
452/* Helpers for subdevices. If the associated ctrl_handler == NULL then they
453 will all return -EINVAL. */
454int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
455int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
456int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
457int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
458int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
459int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
460int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
461
462#endif