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/mutex.h>
26 #include <linux/videodev2.h>
27
28 /* forward references */
29 struct v4l2_ctrl_handler;
30 struct v4l2_ctrl_helper;
31 struct v4l2_ctrl;
32 struct video_device;
33 struct v4l2_subdev;
34 struct v4l2_subscribed_event;
35 struct v4l2_fh;
36 struct poll_table_struct;
37
38 /** struct v4l2_ctrl_ops - The control operations that the driver has to provide.
39 * @g_volatile_ctrl: Get a new value for this control. Generally only relevant
40 * for volatile (and usually read-only) controls such as a control
41 * that returns the current signal strength which changes
42 * continuously.
43 * If not set, then the currently cached value will be returned.
44 * @try_ctrl: Test whether the control's value is valid. Only relevant when
45 * the usual min/max/step checks are not sufficient.
46 * @s_ctrl: Actually set the new control value. s_ctrl is compulsory. The
47 * ctrl->handler->lock is held when these ops are called, so no
48 * one else can access controls owned by that handler.
49 */
50 struct v4l2_ctrl_ops {
51 int (*g_volatile_ctrl)(struct v4l2_ctrl *ctrl);
52 int (*try_ctrl)(struct v4l2_ctrl *ctrl);
53 int (*s_ctrl)(struct v4l2_ctrl *ctrl);
54 };
55
56 /** struct v4l2_ctrl - The control structure.
57 * @node: The list node.
58 * @ev_subs: The list of control event subscriptions.
59 * @handler: The handler that owns the control.
60 * @cluster: Point to start of cluster array.
61 * @ncontrols: Number of controls in cluster array.
62 * @done: Internal flag: set for each processed control.
63 * @is_new: Set when the user specified a new value for this control. It
64 * is also set when called from v4l2_ctrl_handler_setup. Drivers
65 * should never set this flag.
66 * @is_private: If set, then this control is private to its handler and it
67 * will not be added to any other handlers. Drivers can set
68 * this flag.
69 * @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 * @has_volatiles: If set, then one or more members of the cluster are volatile.
74 * Drivers should never touch this flag.
75 * @manual_mode_value: If the is_auto flag is set, then this is the value
76 * of the auto control that determines if that control is in
77 * manual mode. So if the value of the auto control equals this
78 * value, then the whole cluster is in manual mode. Drivers should
79 * never set this flag directly.
80 * @ops: The control ops.
81 * @id: The control ID.
82 * @name: The control name.
83 * @type: The control type.
84 * @minimum: The control's minimum value.
85 * @maximum: The control's maximum value.
86 * @default_value: The control's default value.
87 * @step: The control's step value for non-menu controls.
88 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
89 * easy to skip menu items that are not valid. If bit X is set,
90 * then menu item X is skipped. Of course, this only works for
91 * menus with <= 32 menu items. There are no menus that come
92 * close to that number, so this is OK. Should we ever need more,
93 * then this will have to be extended to a u64 or a bit array.
94 * @qmenu: A const char * array for all menu items. Array entries that are
95 * empty strings ("") correspond to non-existing menu items (this
96 * is in addition to the menu_skip_mask above). The last entry
97 * must be NULL.
98 * @flags: The control's flags.
99 * @cur: The control's current value.
100 * @val: The control's new s32 value.
101 * @val64: The control's new s64 value.
102 * @string: The control's new string value.
103 * @priv: The control's private pointer. For use by the driver. It is
104 * untouched by the control framework. Note that this pointer is
105 * not freed when the control is deleted. Should this be needed
106 * then a new internal bitfield can be added to tell the framework
107 * to free this pointer.
108 */
109 struct v4l2_ctrl {
110 /* Administrative fields */
111 struct list_head node;
112 struct list_head ev_subs;
113 struct v4l2_ctrl_handler *handler;
114 struct v4l2_ctrl **cluster;
115 unsigned ncontrols;
116 unsigned int done:1;
117
118 unsigned int is_new:1;
119 unsigned int is_private:1;
120 unsigned int is_auto:1;
121 unsigned int has_volatiles:1;
122 unsigned int manual_mode_value:8;
123
124 const struct v4l2_ctrl_ops *ops;
125 u32 id;
126 const char *name;
127 enum v4l2_ctrl_type type;
128 s32 minimum, maximum, default_value;
129 union {
130 u32 step;
131 u32 menu_skip_mask;
132 };
133 const char * const *qmenu;
134 unsigned long flags;
135 union {
136 s32 val;
137 s64 val64;
138 char *string;
139 } cur;
140 union {
141 s32 val;
142 s64 val64;
143 char *string;
144 };
145 void *priv;
146 };
147
148 /** struct v4l2_ctrl_ref - The control reference.
149 * @node: List node for the sorted list.
150 * @next: Single-link list node for the hash.
151 * @ctrl: The actual control information.
152 * @helper: Pointer to helper struct. Used internally in prepare_ext_ctrls().
153 *
154 * Each control handler has a list of these refs. The list_head is used to
155 * keep a sorted-by-control-ID list of all controls, while the next pointer
156 * is used to link the control in the hash's bucket.
157 */
158 struct v4l2_ctrl_ref {
159 struct list_head node;
160 struct v4l2_ctrl_ref *next;
161 struct v4l2_ctrl *ctrl;
162 struct v4l2_ctrl_helper *helper;
163 };
164
165 /** struct v4l2_ctrl_handler - The control handler keeps track of all the
166 * controls: both the controls owned by the handler and those inherited
167 * from other handlers.
168 * @lock: Lock to control access to this handler and its controls.
169 * @ctrls: The list of controls owned by this handler.
170 * @ctrl_refs: The list of control references.
171 * @cached: The last found control reference. It is common that the same
172 * control is needed multiple times, so this is a simple
173 * optimization.
174 * @buckets: Buckets for the hashing. Allows for quick control lookup.
175 * @nr_of_buckets: Total number of buckets in the array.
176 * @error: The error code of the first failed control addition.
177 */
178 struct v4l2_ctrl_handler {
179 struct mutex lock;
180 struct list_head ctrls;
181 struct list_head ctrl_refs;
182 struct v4l2_ctrl_ref *cached;
183 struct v4l2_ctrl_ref **buckets;
184 u16 nr_of_buckets;
185 int error;
186 };
187
188 /** struct v4l2_ctrl_config - Control configuration structure.
189 * @ops: The control ops.
190 * @id: The control ID.
191 * @name: The control name.
192 * @type: The control type.
193 * @min: The control's minimum value.
194 * @max: The control's maximum value.
195 * @step: The control's step value for non-menu controls.
196 * @def: The control's default value.
197 * @flags: The control's flags.
198 * @menu_skip_mask: The control's skip mask for menu controls. This makes it
199 * easy to skip menu items that are not valid. If bit X is set,
200 * then menu item X is skipped. Of course, this only works for
201 * menus with <= 32 menu items. There are no menus that come
202 * close to that number, so this is OK. Should we ever need more,
203 * then this will have to be extended to a u64 or a bit array.
204 * @qmenu: A const char * array for all menu items. Array entries that are
205 * empty strings ("") correspond to non-existing menu items (this
206 * is in addition to the menu_skip_mask above). The last entry
207 * must be NULL.
208 * @is_private: If set, then this control is private to its handler and it
209 * will not be added to any other handlers.
210 */
211 struct v4l2_ctrl_config {
212 const struct v4l2_ctrl_ops *ops;
213 u32 id;
214 const char *name;
215 enum v4l2_ctrl_type type;
216 s32 min;
217 s32 max;
218 u32 step;
219 s32 def;
220 u32 flags;
221 u32 menu_skip_mask;
222 const char * const *qmenu;
223 unsigned int is_private:1;
224 };
225
226 /** v4l2_ctrl_fill() - Fill in the control fields based on the control ID.
227 *
228 * This works for all standard V4L2 controls.
229 * For non-standard controls it will only fill in the given arguments
230 * and @name will be NULL.
231 *
232 * This function will overwrite the contents of @name, @type and @flags.
233 * The contents of @min, @max, @step and @def may be modified depending on
234 * the type.
235 *
236 * Do not use in drivers! It is used internally for backwards compatibility
237 * control handling only. Once all drivers are converted to use the new
238 * control framework this function will no longer be exported.
239 */
240 void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
241 s32 *min, s32 *max, s32 *step, s32 *def, u32 *flags);
242
243
244 /** v4l2_ctrl_handler_init() - Initialize the control handler.
245 * @hdl: The control handler.
246 * @nr_of_controls_hint: A hint of how many controls this handler is
247 * expected to refer to. This is the total number, so including
248 * any inherited controls. It doesn't have to be precise, but if
249 * it is way off, then you either waste memory (too many buckets
250 * are allocated) or the control lookup becomes slower (not enough
251 * buckets are allocated, so there are more slow list lookups).
252 * It will always work, though.
253 *
254 * Returns an error if the buckets could not be allocated. This error will
255 * also be stored in @hdl->error.
256 */
257 int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
258 unsigned nr_of_controls_hint);
259
260 /** v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
261 * the control list.
262 * @hdl: The control handler.
263 *
264 * Does nothing if @hdl == NULL.
265 */
266 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl);
267
268 /** v4l2_ctrl_handler_setup() - Call the s_ctrl op for all controls belonging
269 * to the handler to initialize the hardware to the current control values.
270 * @hdl: The control handler.
271 *
272 * Button controls will be skipped, as are read-only controls.
273 *
274 * If @hdl == NULL, then this just returns 0.
275 */
276 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl);
277
278 /** v4l2_ctrl_handler_log_status() - Log all controls owned by the handler.
279 * @hdl: The control handler.
280 * @prefix: The prefix to use when logging the control values. If the
281 * prefix does not end with a space, then ": " will be added
282 * after the prefix. If @prefix == NULL, then no prefix will be
283 * used.
284 *
285 * For use with VIDIOC_LOG_STATUS.
286 *
287 * Does nothing if @hdl == NULL.
288 */
289 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
290 const char *prefix);
291
292 /** v4l2_ctrl_new_custom() - Allocate and initialize a new custom V4L2
293 * control.
294 * @hdl: The control handler.
295 * @cfg: The control's configuration data.
296 * @priv: The control's driver-specific private data.
297 *
298 * If the &v4l2_ctrl struct could not be allocated then NULL is returned
299 * and @hdl->error is set to the error code (if it wasn't set already).
300 */
301 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
302 const struct v4l2_ctrl_config *cfg, void *priv);
303
304 /** v4l2_ctrl_new_std() - Allocate and initialize a new standard V4L2 non-menu control.
305 * @hdl: The control handler.
306 * @ops: The control ops.
307 * @id: The control ID.
308 * @min: The control's minimum value.
309 * @max: The control's maximum value.
310 * @step: The control's step value
311 * @def: The control's default value.
312 *
313 * If the &v4l2_ctrl struct could not be allocated, or the control
314 * ID is not known, then NULL is returned and @hdl->error is set to the
315 * appropriate error code (if it wasn't set already).
316 *
317 * If @id refers to a menu control, then this function will return NULL.
318 *
319 * Use v4l2_ctrl_new_std_menu() when adding menu controls.
320 */
321 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
322 const struct v4l2_ctrl_ops *ops,
323 u32 id, s32 min, s32 max, u32 step, s32 def);
324
325 /** v4l2_ctrl_new_std_menu() - Allocate and initialize a new standard V4L2 menu control.
326 * @hdl: The control handler.
327 * @ops: The control ops.
328 * @id: The control ID.
329 * @max: The control's maximum value.
330 * @mask: The control's skip mask for menu controls. This makes it
331 * easy to skip menu items that are not valid. If bit X is set,
332 * then menu item X is skipped. Of course, this only works for
333 * menus with <= 32 menu items. There are no menus that come
334 * close to that number, so this is OK. Should we ever need more,
335 * then this will have to be extended to a u64 or a bit array.
336 * @def: The control's default value.
337 *
338 * Same as v4l2_ctrl_new_std(), but @min is set to 0 and the @mask value
339 * determines which menu items are to be skipped.
340 *
341 * If @id refers to a non-menu control, then this function will return NULL.
342 */
343 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
344 const struct v4l2_ctrl_ops *ops,
345 u32 id, s32 max, s32 mask, s32 def);
346
347 /** v4l2_ctrl_add_ctrl() - Add a control from another handler to this handler.
348 * @hdl: The control handler.
349 * @ctrl: The control to add.
350 *
351 * It will return NULL if it was unable to add the control reference.
352 * If the control already belonged to the handler, then it will do
353 * nothing and just return @ctrl.
354 */
355 struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
356 struct v4l2_ctrl *ctrl);
357
358 /** v4l2_ctrl_add_handler() - Add all controls from handler @add to
359 * handler @hdl.
360 * @hdl: The control handler.
361 * @add: The control handler whose controls you want to add to
362 * the @hdl control handler.
363 *
364 * Does nothing if either of the two is a NULL pointer.
365 * In case of an error @hdl->error will be set to the error code (if it
366 * wasn't set already).
367 */
368 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
369 struct v4l2_ctrl_handler *add);
370
371
372 /** v4l2_ctrl_cluster() - Mark all controls in the cluster as belonging to that cluster.
373 * @ncontrols: The number of controls in this cluster.
374 * @controls: The cluster control array of size @ncontrols.
375 */
376 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls);
377
378
379 /** v4l2_ctrl_auto_cluster() - Mark all controls in the cluster as belonging to
380 * that cluster and set it up for autofoo/foo-type handling.
381 * @ncontrols: The number of controls in this cluster.
382 * @controls: The cluster control array of size @ncontrols. The first control
383 * must be the 'auto' control (e.g. autogain, autoexposure, etc.)
384 * @manual_val: The value for the first control in the cluster that equals the
385 * manual setting.
386 * @set_volatile: If true, then all controls except the first auto control will
387 * be volatile.
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 */
408 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
409 u8 manual_val, bool set_volatile);
410
411
412 /** 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 */
419 struct 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 * The V4L2_EVENT_CTRL event will be generated afterwards.
429 *
430 * This function assumes that the control handler is locked.
431 */
432 void 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 * The V4L2_EVENT_CTRL event will be generated afterwards.
441 * This will usually be called when starting or stopping streaming in the
442 * driver.
443 *
444 * This function assumes that the control handler is not locked and will
445 * take the lock itself.
446 */
447 void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed);
448
449 /** v4l2_ctrl_lock() - Helper function to lock the handler
450 * associated with the control.
451 * @ctrl: The control to lock.
452 */
v4l2_ctrl_lock(struct v4l2_ctrl * ctrl)453 static inline void v4l2_ctrl_lock(struct v4l2_ctrl *ctrl)
454 {
455 mutex_lock(&ctrl->handler->lock);
456 }
457
458 /** v4l2_ctrl_lock() - Helper function to unlock the handler
459 * associated with the control.
460 * @ctrl: The control to unlock.
461 */
v4l2_ctrl_unlock(struct v4l2_ctrl * ctrl)462 static inline void v4l2_ctrl_unlock(struct v4l2_ctrl *ctrl)
463 {
464 mutex_unlock(&ctrl->handler->lock);
465 }
466
467 /** v4l2_ctrl_g_ctrl() - Helper function to get the control's value from within a driver.
468 * @ctrl: The control.
469 *
470 * This returns the control's value safely by going through the control
471 * framework. This function will lock the control's handler, so it cannot be
472 * used from within the &v4l2_ctrl_ops functions.
473 *
474 * This function is for integer type controls only.
475 */
476 s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
477
478 /** v4l2_ctrl_s_ctrl() - Helper function to set the control's value from within a driver.
479 * @ctrl: The control.
480 * @val: The new value.
481 *
482 * This set the control's new value safely by going through the control
483 * framework. This function will lock the control's handler, so it cannot be
484 * used from within the &v4l2_ctrl_ops functions.
485 *
486 * This function is for integer type controls only.
487 */
488 int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
489
490 /* Internal helper functions that deal with control events. */
491 void v4l2_ctrl_add_event(struct v4l2_ctrl *ctrl,
492 struct v4l2_subscribed_event *sev);
493 void v4l2_ctrl_del_event(struct v4l2_ctrl *ctrl,
494 struct v4l2_subscribed_event *sev);
495
496 /* Can be used as a vidioc_log_status function that just dumps all controls
497 associated with the filehandle. */
498 int v4l2_ctrl_log_status(struct file *file, void *fh);
499
500 /* Can be used as a vidioc_subscribe_event function that just subscribes
501 control events. */
502 int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
503 struct v4l2_event_subscription *sub);
504
505 /* Can be used as a poll function that just polls for control events. */
506 unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait);
507
508 /* Helpers for ioctl_ops. If hdl == NULL then they will all return -EINVAL. */
509 int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc);
510 int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm);
511 int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *ctrl);
512 int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
513 struct v4l2_control *ctrl);
514 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
515 int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *c);
516 int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
517 struct v4l2_ext_controls *c);
518
519 /* Helpers for subdevices. If the associated ctrl_handler == NULL then they
520 will all return -EINVAL. */
521 int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
522 int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
523 int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
524 int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
525 int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
526 int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
527 int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
528
529 #endif
530