[media] v4l2-ctrls: simplify event subscription
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / media / v4l2-ctrls.h
blobde68a59c7d845b318e03ded06b8f3c4c2d578d6e
1 /*
2 V4L2 controls support header.
4 Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
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.
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.
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
21 #ifndef _V4L2_CTRLS_H
22 #define _V4L2_CTRLS_H
24 #include <linux/list.h>
25 #include <linux/device.h>
26 #include <linux/videodev2.h>
28 /* forward references */
29 struct v4l2_ctrl_handler;
30 struct v4l2_ctrl;
31 struct video_device;
32 struct v4l2_subdev;
33 struct v4l2_event_subscription;
34 struct v4l2_fh;
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.
48 struct 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);
54 /** struct v4l2_ctrl - The control structure.
55 * @node: The list node.
56 * @handler: The handler that owns the control.
57 * @cluster: Point to start of cluster array.
58 * @ncontrols: Number of controls in cluster array.
59 * @done: Internal flag: set for each processed control.
60 * @is_new: Set when the user specified a new value for this control. It
61 * is also set when called from v4l2_ctrl_handler_setup. Drivers
62 * should never set this flag.
63 * @is_private: If set, then this control is private to its handler and it
64 * will not be added to any other handlers. Drivers can set
65 * this flag.
66 * @is_volatile: If set, then this control is volatile. This means that the
67 * control's current value cannot be cached and needs to be
68 * retrieved through the g_volatile_ctrl op. Drivers can set
69 * this flag.
70 * @is_auto: If set, then this control selects whether the other cluster
71 * members are in 'automatic' mode or 'manual' mode. This is
72 * used for autogain/gain type clusters. Drivers should never
73 * set this flag directly.
74 * @manual_mode_value: If the is_auto flag is set, then this is the value
75 * of the auto control that determines if that control is in
76 * manual mode. So if the value of the auto control equals this
77 * value, then the whole cluster is in manual mode. Drivers should
78 * never set this flag directly.
79 * @ops: The control ops.
80 * @id: The control ID.
81 * @name: The control name.
82 * @type: The control type.
83 * @minimum: The control's minimum value.
84 * @maximum: The control's maximum value.
85 * @default_value: The control's default value.
86 * @step: The control's step value for non-menu controls.
87 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
88 * easy to skip menu items that are not valid. If bit X is set,
89 * then menu item X is skipped. Of course, this only works for
90 * menus with <= 32 menu items. There are no menus that come
91 * close to that number, so this is OK. Should we ever need more,
92 * then this will have to be extended to a u64 or a bit array.
93 * @qmenu: A const char * array for all menu items. Array entries that are
94 * empty strings ("") correspond to non-existing menu items (this
95 * is in addition to the menu_skip_mask above). The last entry
96 * must be NULL.
97 * @flags: The control's flags.
98 * @cur: The control's current value.
99 * @val: The control's new s32 value.
100 * @val64: The control's new s64 value.
101 * @string: The control's new string value.
102 * @priv: The control's private pointer. For use by the driver. It is
103 * untouched by the control framework. Note that this pointer is
104 * not freed when the control is deleted. Should this be needed
105 * then a new internal bitfield can be added to tell the framework
106 * to free this pointer.
108 struct v4l2_ctrl {
109 /* Administrative fields */
110 struct list_head node;
111 struct list_head fhs;
112 struct v4l2_ctrl_handler *handler;
113 struct v4l2_ctrl **cluster;
114 unsigned ncontrols;
115 unsigned int done:1;
117 unsigned int is_new:1;
118 unsigned int is_private:1;
119 unsigned int is_volatile:1;
120 unsigned int is_auto:1;
121 unsigned int manual_mode_value:5;
123 const struct v4l2_ctrl_ops *ops;
124 u32 id;
125 const char *name;
126 enum v4l2_ctrl_type type;
127 s32 minimum, maximum, default_value;
128 union {
129 u32 step;
130 u32 menu_skip_mask;
132 const char * const *qmenu;
133 unsigned long flags;
134 union {
135 s32 val;
136 s64 val64;
137 char *string;
138 } cur;
139 union {
140 s32 val;
141 s64 val64;
142 char *string;
144 void *priv;
147 /** struct v4l2_ctrl_ref - The control reference.
148 * @node: List node for the sorted list.
149 * @next: Single-link list node for the hash.
150 * @ctrl: The actual control information.
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.
156 struct v4l2_ctrl_ref {
157 struct list_head node;
158 struct v4l2_ctrl_ref *next;
159 struct v4l2_ctrl *ctrl;
162 /** struct v4l2_ctrl_handler - The control handler keeps track of all the
163 * controls: both the controls owned by the handler and those inherited
164 * from other handlers.
165 * @lock: Lock to control access to this handler and its controls.
166 * @ctrls: The list of controls owned by this handler.
167 * @ctrl_refs: The list of control references.
168 * @cached: The last found control reference. It is common that the same
169 * control is needed multiple times, so this is a simple
170 * optimization.
171 * @buckets: Buckets for the hashing. Allows for quick control lookup.
172 * @nr_of_refs: Total number of control references in the list.
173 * @nr_of_buckets: Total number of buckets in the array.
174 * @error: The error code of the first failed control addition.
176 struct 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_refs;
183 u16 nr_of_buckets;
184 int error;
187 struct v4l2_ctrl_fh {
188 struct list_head node;
189 struct v4l2_fh *fh;
192 /** struct v4l2_ctrl_config - Control configuration structure.
193 * @ops: The control ops.
194 * @id: The control ID.
195 * @name: The control name.
196 * @type: The control type.
197 * @min: The control's minimum value.
198 * @max: The control's maximum value.
199 * @step: The control's step value for non-menu controls.
200 * @def: The control's default value.
201 * @flags: The control's flags.
202 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
203 * easy to skip menu items that are not valid. If bit X is set,
204 * then menu item X is skipped. Of course, this only works for
205 * menus with <= 32 menu items. There are no menus that come
206 * close to that number, so this is OK. Should we ever need more,
207 * then this will have to be extended to a u64 or a bit array.
208 * @qmenu: A const char * array for all menu items. Array entries that are
209 * empty strings ("") correspond to non-existing menu items (this
210 * is in addition to the menu_skip_mask above). The last entry
211 * must be NULL.
212 * @is_private: If set, then this control is private to its handler and it
213 * will not be added to any other handlers.
214 * @is_volatile: If set, then this control is volatile. This means that the
215 * control's current value cannot be cached and needs to be
216 * retrieved through the g_volatile_ctrl op.
218 struct v4l2_ctrl_config {
219 const struct v4l2_ctrl_ops *ops;
220 u32 id;
221 const char *name;
222 enum v4l2_ctrl_type type;
223 s32 min;
224 s32 max;
225 u32 step;
226 s32 def;
227 u32 flags;
228 u32 menu_skip_mask;
229 const char * const *qmenu;
230 unsigned int is_private:1;
231 unsigned int is_volatile:1;
234 /** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
236 * This works for all standard V4L2 controls.
237 * For non-standard controls it will only fill in the given arguments
238 * and @name will be NULL.
240 * This function will overwrite the contents of @name, @type and @flags.
241 * The contents of @min, @max, @step and @def may be modified depending on
242 * the type.
244 * Do not use in drivers! It is used internally for backwards compatibility
245 * control handling only. Once all drivers are converted to use the new
246 * control framework this function will no longer be exported.
248 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
249 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags);
252 /** v4l2_ctrl_handler_init() - Initialize the control handler.
253 * @hdl: The control handler.
254 * @nr_of_controls_hint: A hint of how many controls this handler is
255 * expected to refer to. This is the total number, so including
256 * any inherited controls. It doesn't have to be precise, but if
257 * it is way off, then you either waste memory (too many buckets
258 * are allocated) or the control lookup becomes slower (not enough
259 * buckets are allocated, so there are more slow list lookups).
260 * It will always work, though.
262 * Returns an error if the buckets could not be allocated. This error will
263 * also be stored in @hdl->error.
265 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
266 unsigned nr_of_controls_hint);
268 /** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
269 * the control list.
270 * @hdl: The control handler.
272 * Does nothing if @hdl == NULL.
274 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
276 /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
277 * to the handler to initialize the hardware to the current control values.
278 * @hdl: The control handler.
280 * Button controls will be skipped, as are read-only controls.
282 * If @hdl == NULL, then this just returns 0.
284 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
286 /** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
287 * @hdl: The control handler.
288 * @prefix: The prefix to use when logging the control values. If the
289 * prefix does not end with a space, then ": " will be added
290 * after the prefix. If @prefix == NULL, then no prefix will be
291 * used.
293 * For use with VIDIOC_LOG_STATUS.
295 * Does nothing if @hdl == NULL.
297 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
298 const char *prefix);
300 /** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
301 * control.
302 * @hdl: The control handler.
303 * @cfg: The control's configuration data.
304 * @priv: The control's driver-specific private data.
306 * If the &v4l2_ctrl struct could not be allocated then NULL is returned
307 * and @hdl->error is set to the error code (if it wasn't set already).
309 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
310 const struct v4l2_ctrl_config *cfg, void *priv);
312 /** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
313 * @hdl: The control handler.
314 * @ops: The control ops.
315 * @id: The control ID.
316 * @min: The control's minimum value.
317 * @max: The control's maximum value.
318 * @step: The control's step value
319 * @def: The control's default value.
321 * If the &v4l2_ctrl struct could not be allocated, or the control
322 * ID is not known, then NULL is returned and @hdl->error is set to the
323 * appropriate error code (if it wasn't set already).
325 * If @id refers to a menu control, then this function will return NULL.
327 * Use v4l2_ctrl_new_std_menu() when adding menu controls.
329 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
330 const struct v4l2_ctrl_ops *ops,
331 u32 id, s32 min, s32 max, u32 step, s32 def);
333 /** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
334 * @hdl: The control handler.
335 * @ops: The control ops.
336 * @id: The control ID.
337 * @max: The control's maximum value.
338 * @mask: The control's skip mask for menu controls. This makes it
339 * easy to skip menu items that are not valid. If bit X is set,
340 * then menu item X is skipped. Of course, this only works for
341 * menus with <= 32 menu items. There are no menus that come
342 * close to that number, so this is OK. Should we ever need more,
343 * then this will have to be extended to a u64 or a bit array.
344 * @def: The control's default value.
346 * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
347 * determines which menu items are to be skipped.
349 * If @id refers to a non-menu control, then this function will return NULL.
351 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
352 const struct v4l2_ctrl_ops *ops,
353 u32 id, s32 max, s32 mask, s32 def);
355 /** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
356 * @hdl: The control handler.
357 * @ctrl: The control to add.
359 * It will return NULL if it was unable to add the control reference.
360 * If the control already belonged to the handler, then it will do
361 * nothing and just return @ctrl.
363 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
364 struct v4l2_ctrl *ctrl);
366 /** v4l2_ctrl_add_handler() - Add all controls from handler @add to
367 * handler @hdl.
368 * @hdl: The control handler.
369 * @add: The control handler whose controls you want to add to
370 * the @hdl control handler.
372 * Does nothing if either of the two is a NULL pointer.
373 * In case of an error @hdl->error will be set to the error code (if it
374 * wasn't set already).
376 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
377 struct v4l2_ctrl_handler *add);
380 /** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
381 * @ncontrols: The number of controls in this cluster.
382 * @controls: The cluster control array of size @ncontrols.
384 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
387 /** v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
388 * that cluster and set it up for autofoo/foo-type handling.
389 * @ncontrols: The number of controls in this cluster.
390 * @controls: The cluster control array of size @ncontrols. The first control
391 * must be the 'auto' control (e.g. autogain, autoexposure, etc.)
392 * @manual_val: The value for the first control in the cluster that equals the
393 * manual setting.
394 * @set_volatile: If true, then all controls except the first auto control will
395 * have is_volatile set to true. If false, then is_volatile will not
396 * be touched.
398 * Use for control groups where one control selects some automatic feature and
399 * the other controls are only active whenever the automatic feature is turned
400 * off (manual mode). Typical examples: autogain vs gain, auto-whitebalance vs
401 * red and blue balance, etc.
403 * The behavior of such controls is as follows:
405 * When the autofoo control is set to automatic, then any manual controls
406 * are set to inactive and any reads will call g_volatile_ctrl (if the control
407 * was marked volatile).
409 * When the autofoo control is set to manual, then any manual controls will
410 * be marked active, and any reads will just return the current value without
411 * going through g_volatile_ctrl.
413 * In addition, this function will set the V4L2_CTRL_FLAG_UPDATE flag
414 * on the autofoo control and V4L2_CTRL_FLAG_INACTIVE on the foo control(s)
415 * if autofoo is in auto mode.
417 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
418 u8 manual_val, bool set_volatile);
421 /** v4l2_ctrl_find() - Find a control with the given ID.
422 * @hdl: The control handler.
423 * @id: The control ID to find.
425 * If @hdl == NULL this will return NULL as well. Will lock the handler so
426 * do not use from inside &v4l2_ctrl_ops.
428 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id);
430 /** v4l2_ctrl_activate() - Make the control active or inactive.
431 * @ctrl: The control to (de)activate.
432 * @active: True if the control should become active.
434 * This sets or clears the V4L2_CTRL_FLAG_INACTIVE flag atomically.
435 * Does nothing if @ctrl == NULL.
436 * This will usually be called from within the s_ctrl op.
437 * The V4L2_EVENT_CTRL event will be generated afterwards.
439 * This function assumes that the control handler is locked.
441 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active);
443 /** v4l2_ctrl_grab() - Mark the control as grabbed or not grabbed.
444 * @ctrl: The control to (de)activate.
445 * @grabbed: True if the control should become grabbed.
447 * This sets or clears the V4L2_CTRL_FLAG_GRABBED flag atomically.
448 * Does nothing if @ctrl == NULL.
449 * The V4L2_EVENT_CTRL event will be generated afterwards.
450 * This will usually be called when starting or stopping streaming in the
451 * driver.
453 * This function assumes that the control handler is not locked and will
454 * take the lock itself.
456 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
458 /** v4l2_ctrl_lock() - Helper function to lock the handler
459 * associated with the control.
460 * @ctrl: The control to lock.
462 static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
464 mutex_lock(&ctrl->handler->lock);
467 /** v4l2_ctrl_lock() - Helper function to unlock the handler
468 * associated with the control.
469 * @ctrl: The control to unlock.
471 static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
473 mutex_unlock(&ctrl->handler->lock);
476 /** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
477 * @ctrl: The control.
479 * This returns the control's value safely by going through the control
480 * framework. This function will lock the control's handler, so it cannot be
481 * used from within the &v4l2_ctrl_ops functions.
483 * This function is for integer type controls only.
485 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
487 /** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
488 * @ctrl: The control.
489 * @val: The new value.
491 * This set the control's new value safely by going through the control
492 * framework. This function will lock the control's handler, so it cannot be
493 * used from within the &v4l2_ctrl_ops functions.
495 * This function is for integer type controls only.
497 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
499 /* Internal helper functions that deal with control events. */
500 void v4l2_ctrl_add_fh(struct v4l2_ctrl_handler *hdl,
501 struct v4l2_ctrl_fh *ctrl_fh,
502 struct v4l2_event_subscription *sub);
503 void v4l2_ctrl_del_fh(struct v4l2_ctrl *ctrl, struct v4l2_fh *fh);
505 /** v4l2_ctrl_subscribe_fh() - Helper function that subscribes a control event.
506 * @fh: The file handler that subscribed the control event.
507 * @sub: The event to subscribe (type must be V4L2_EVENT_CTRL).
508 * @n: How many events should be allocated? (Passed to v4l2_event_alloc).
509 * Recommended to set to twice the number of controls plus whatever
510 * is needed for other events. This function will set n to
511 * max(n, 2 * fh->ctrl_handler->nr_of_refs).
513 * A helper function that initializes the fh for events, allocates the
514 * list of events and subscribes the control event.
516 * Typically called in the handler of VIDIOC_SUBSCRIBE_EVENT in the
517 * V4L2_EVENT_CTRL case.
519 int v4l2_ctrl_subscribe_fh(struct v4l2_fh *fh,
520 struct v4l2_event_subscription *sub, unsigned n);
522 /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
523 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
524 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
525 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
526 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
527 struct v4l2_control *ctrl);
528 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
529 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
530 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
531 struct v4l2_ext_controls *c);
533 /* Helpers for subdevices. If the associated ctrl_handler == NULL then they
534 will all return -EINVAL. */
535 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
536 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
537 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
538 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
539 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
540 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
541 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
543 #endif