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