xref: /openbmc/linux/Documentation/driver-api/media/v4l2-controls.rst (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1ff768f59SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
2ff768f59SMauro Carvalho Chehab
3ff768f59SMauro Carvalho ChehabV4L2 Controls
4ff768f59SMauro Carvalho Chehab=============
5ff768f59SMauro Carvalho Chehab
6ff768f59SMauro Carvalho ChehabIntroduction
7ff768f59SMauro Carvalho Chehab------------
8ff768f59SMauro Carvalho Chehab
9ff768f59SMauro Carvalho ChehabThe V4L2 control API seems simple enough, but quickly becomes very hard to
10ff768f59SMauro Carvalho Chehabimplement correctly in drivers. But much of the code needed to handle controls
11ff768f59SMauro Carvalho Chehabis actually not driver specific and can be moved to the V4L core framework.
12ff768f59SMauro Carvalho Chehab
13ff768f59SMauro Carvalho ChehabAfter all, the only part that a driver developer is interested in is:
14ff768f59SMauro Carvalho Chehab
15ff768f59SMauro Carvalho Chehab1) How do I add a control?
16ff768f59SMauro Carvalho Chehab2) How do I set the control's value? (i.e. s_ctrl)
17ff768f59SMauro Carvalho Chehab
18ff768f59SMauro Carvalho ChehabAnd occasionally:
19ff768f59SMauro Carvalho Chehab
20ff768f59SMauro Carvalho Chehab3) How do I get the control's value? (i.e. g_volatile_ctrl)
21ff768f59SMauro Carvalho Chehab4) How do I validate the user's proposed control value? (i.e. try_ctrl)
22ff768f59SMauro Carvalho Chehab
23ff768f59SMauro Carvalho ChehabAll the rest is something that can be done centrally.
24ff768f59SMauro Carvalho Chehab
25ff768f59SMauro Carvalho ChehabThe control framework was created in order to implement all the rules of the
26ff768f59SMauro Carvalho ChehabV4L2 specification with respect to controls in a central place. And to make
27ff768f59SMauro Carvalho Chehablife as easy as possible for the driver developer.
28ff768f59SMauro Carvalho Chehab
29ff768f59SMauro Carvalho ChehabNote that the control framework relies on the presence of a struct
309303c9d5SMauro Carvalho Chehab:c:type:`v4l2_device` for V4L2 drivers and struct v4l2_subdev for
31ff768f59SMauro Carvalho Chehabsub-device drivers.
32ff768f59SMauro Carvalho Chehab
33ff768f59SMauro Carvalho Chehab
34ff768f59SMauro Carvalho ChehabObjects in the framework
35ff768f59SMauro Carvalho Chehab------------------------
36ff768f59SMauro Carvalho Chehab
37ff768f59SMauro Carvalho ChehabThere are two main objects:
38ff768f59SMauro Carvalho Chehab
39ff768f59SMauro Carvalho ChehabThe :c:type:`v4l2_ctrl` object describes the control properties and keeps
40ff768f59SMauro Carvalho Chehabtrack of the control's value (both the current value and the proposed new
41ff768f59SMauro Carvalho Chehabvalue).
42ff768f59SMauro Carvalho Chehab
43ff768f59SMauro Carvalho Chehab:c:type:`v4l2_ctrl_handler` is the object that keeps track of controls. It
44ff768f59SMauro Carvalho Chehabmaintains a list of v4l2_ctrl objects that it owns and another list of
45ff768f59SMauro Carvalho Chehabreferences to controls, possibly to controls owned by other handlers.
46ff768f59SMauro Carvalho Chehab
47ff768f59SMauro Carvalho Chehab
48ff768f59SMauro Carvalho ChehabBasic usage for V4L2 and sub-device drivers
49ff768f59SMauro Carvalho Chehab-------------------------------------------
50ff768f59SMauro Carvalho Chehab
51ff768f59SMauro Carvalho Chehab1) Prepare the driver:
52ff768f59SMauro Carvalho Chehab
53ff768f59SMauro Carvalho Chehab.. code-block:: c
54ff768f59SMauro Carvalho Chehab
55ff768f59SMauro Carvalho Chehab	#include <media/v4l2-ctrls.h>
56ff768f59SMauro Carvalho Chehab
57ff768f59SMauro Carvalho Chehab1.1) Add the handler to your driver's top-level struct:
58ff768f59SMauro Carvalho Chehab
59ff768f59SMauro Carvalho ChehabFor V4L2 drivers:
60ff768f59SMauro Carvalho Chehab
61ff768f59SMauro Carvalho Chehab.. code-block:: c
62ff768f59SMauro Carvalho Chehab
63ff768f59SMauro Carvalho Chehab	struct foo_dev {
64ff768f59SMauro Carvalho Chehab		...
65ff768f59SMauro Carvalho Chehab		struct v4l2_device v4l2_dev;
66ff768f59SMauro Carvalho Chehab		...
67ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl_handler ctrl_handler;
68ff768f59SMauro Carvalho Chehab		...
69ff768f59SMauro Carvalho Chehab	};
70ff768f59SMauro Carvalho Chehab
71ff768f59SMauro Carvalho ChehabFor sub-device drivers:
72ff768f59SMauro Carvalho Chehab
73ff768f59SMauro Carvalho Chehab.. code-block:: c
74ff768f59SMauro Carvalho Chehab
75ff768f59SMauro Carvalho Chehab	struct foo_dev {
76ff768f59SMauro Carvalho Chehab		...
77ff768f59SMauro Carvalho Chehab		struct v4l2_subdev sd;
78ff768f59SMauro Carvalho Chehab		...
79ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl_handler ctrl_handler;
80ff768f59SMauro Carvalho Chehab		...
81ff768f59SMauro Carvalho Chehab	};
82ff768f59SMauro Carvalho Chehab
83ff768f59SMauro Carvalho Chehab1.2) Initialize the handler:
84ff768f59SMauro Carvalho Chehab
85ff768f59SMauro Carvalho Chehab.. code-block:: c
86ff768f59SMauro Carvalho Chehab
87ff768f59SMauro Carvalho Chehab	v4l2_ctrl_handler_init(&foo->ctrl_handler, nr_of_controls);
88ff768f59SMauro Carvalho Chehab
89ff768f59SMauro Carvalho ChehabThe second argument is a hint telling the function how many controls this
90ff768f59SMauro Carvalho Chehabhandler is expected to handle. It will allocate a hashtable based on this
91ff768f59SMauro Carvalho Chehabinformation. It is a hint only.
92ff768f59SMauro Carvalho Chehab
93ff768f59SMauro Carvalho Chehab1.3) Hook the control handler into the driver:
94ff768f59SMauro Carvalho Chehab
95ff768f59SMauro Carvalho ChehabFor V4L2 drivers:
96ff768f59SMauro Carvalho Chehab
97ff768f59SMauro Carvalho Chehab.. code-block:: c
98ff768f59SMauro Carvalho Chehab
99ff768f59SMauro Carvalho Chehab	foo->v4l2_dev.ctrl_handler = &foo->ctrl_handler;
100ff768f59SMauro Carvalho Chehab
101ff768f59SMauro Carvalho ChehabFor sub-device drivers:
102ff768f59SMauro Carvalho Chehab
103ff768f59SMauro Carvalho Chehab.. code-block:: c
104ff768f59SMauro Carvalho Chehab
105ff768f59SMauro Carvalho Chehab	foo->sd.ctrl_handler = &foo->ctrl_handler;
106ff768f59SMauro Carvalho Chehab
107ff768f59SMauro Carvalho Chehab1.4) Clean up the handler at the end:
108ff768f59SMauro Carvalho Chehab
109ff768f59SMauro Carvalho Chehab.. code-block:: c
110ff768f59SMauro Carvalho Chehab
111ff768f59SMauro Carvalho Chehab	v4l2_ctrl_handler_free(&foo->ctrl_handler);
112ff768f59SMauro Carvalho Chehab
113ff768f59SMauro Carvalho Chehab
114ff768f59SMauro Carvalho Chehab2) Add controls:
115ff768f59SMauro Carvalho Chehab
116ff768f59SMauro Carvalho ChehabYou add non-menu controls by calling :c:func:`v4l2_ctrl_new_std`:
117ff768f59SMauro Carvalho Chehab
118ff768f59SMauro Carvalho Chehab.. code-block:: c
119ff768f59SMauro Carvalho Chehab
120ff768f59SMauro Carvalho Chehab	struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
121ff768f59SMauro Carvalho Chehab			const struct v4l2_ctrl_ops *ops,
122ff768f59SMauro Carvalho Chehab			u32 id, s32 min, s32 max, u32 step, s32 def);
123ff768f59SMauro Carvalho Chehab
124ff768f59SMauro Carvalho ChehabMenu and integer menu controls are added by calling
125ff768f59SMauro Carvalho Chehab:c:func:`v4l2_ctrl_new_std_menu`:
126ff768f59SMauro Carvalho Chehab
127ff768f59SMauro Carvalho Chehab.. code-block:: c
128ff768f59SMauro Carvalho Chehab
129ff768f59SMauro Carvalho Chehab	struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
130ff768f59SMauro Carvalho Chehab			const struct v4l2_ctrl_ops *ops,
131ff768f59SMauro Carvalho Chehab			u32 id, s32 max, s32 skip_mask, s32 def);
132ff768f59SMauro Carvalho Chehab
133ff768f59SMauro Carvalho ChehabMenu controls with a driver specific menu are added by calling
134ff768f59SMauro Carvalho Chehab:c:func:`v4l2_ctrl_new_std_menu_items`:
135ff768f59SMauro Carvalho Chehab
136ff768f59SMauro Carvalho Chehab.. code-block:: c
137ff768f59SMauro Carvalho Chehab
138ff768f59SMauro Carvalho Chehab       struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(
139ff768f59SMauro Carvalho Chehab                       struct v4l2_ctrl_handler *hdl,
140ff768f59SMauro Carvalho Chehab                       const struct v4l2_ctrl_ops *ops, u32 id, s32 max,
141ff768f59SMauro Carvalho Chehab                       s32 skip_mask, s32 def, const char * const *qmenu);
142ff768f59SMauro Carvalho Chehab
143ff768f59SMauro Carvalho ChehabStandard compound controls can be added by calling
144ff768f59SMauro Carvalho Chehab:c:func:`v4l2_ctrl_new_std_compound`:
145ff768f59SMauro Carvalho Chehab
146ff768f59SMauro Carvalho Chehab.. code-block:: c
147ff768f59SMauro Carvalho Chehab
148ff768f59SMauro Carvalho Chehab       struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl,
149ff768f59SMauro Carvalho Chehab                       const struct v4l2_ctrl_ops *ops, u32 id,
150ff768f59SMauro Carvalho Chehab                       const union v4l2_ctrl_ptr p_def);
151ff768f59SMauro Carvalho Chehab
152ff768f59SMauro Carvalho ChehabInteger menu controls with a driver specific menu can be added by calling
153ff768f59SMauro Carvalho Chehab:c:func:`v4l2_ctrl_new_int_menu`:
154ff768f59SMauro Carvalho Chehab
155ff768f59SMauro Carvalho Chehab.. code-block:: c
156ff768f59SMauro Carvalho Chehab
157ff768f59SMauro Carvalho Chehab	struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
158ff768f59SMauro Carvalho Chehab			const struct v4l2_ctrl_ops *ops,
159ff768f59SMauro Carvalho Chehab			u32 id, s32 max, s32 def, const s64 *qmenu_int);
160ff768f59SMauro Carvalho Chehab
161ff768f59SMauro Carvalho ChehabThese functions are typically called right after the
162ff768f59SMauro Carvalho Chehab:c:func:`v4l2_ctrl_handler_init`:
163ff768f59SMauro Carvalho Chehab
164ff768f59SMauro Carvalho Chehab.. code-block:: c
165ff768f59SMauro Carvalho Chehab
166ff768f59SMauro Carvalho Chehab	static const s64 exp_bias_qmenu[] = {
167ff768f59SMauro Carvalho Chehab	       -2, -1, 0, 1, 2
168ff768f59SMauro Carvalho Chehab	};
169ff768f59SMauro Carvalho Chehab	static const char * const test_pattern[] = {
170ff768f59SMauro Carvalho Chehab		"Disabled",
171ff768f59SMauro Carvalho Chehab		"Vertical Bars",
172ff768f59SMauro Carvalho Chehab		"Solid Black",
173ff768f59SMauro Carvalho Chehab		"Solid White",
174ff768f59SMauro Carvalho Chehab	};
175ff768f59SMauro Carvalho Chehab
176ff768f59SMauro Carvalho Chehab	v4l2_ctrl_handler_init(&foo->ctrl_handler, nr_of_controls);
177ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&foo->ctrl_handler, &foo_ctrl_ops,
178ff768f59SMauro Carvalho Chehab			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
179ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&foo->ctrl_handler, &foo_ctrl_ops,
180ff768f59SMauro Carvalho Chehab			V4L2_CID_CONTRAST, 0, 255, 1, 128);
181ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std_menu(&foo->ctrl_handler, &foo_ctrl_ops,
182ff768f59SMauro Carvalho Chehab			V4L2_CID_POWER_LINE_FREQUENCY,
183ff768f59SMauro Carvalho Chehab			V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
184ff768f59SMauro Carvalho Chehab			V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
185ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_int_menu(&foo->ctrl_handler, &foo_ctrl_ops,
186ff768f59SMauro Carvalho Chehab			V4L2_CID_EXPOSURE_BIAS,
187ff768f59SMauro Carvalho Chehab			ARRAY_SIZE(exp_bias_qmenu) - 1,
188ff768f59SMauro Carvalho Chehab			ARRAY_SIZE(exp_bias_qmenu) / 2 - 1,
189ff768f59SMauro Carvalho Chehab			exp_bias_qmenu);
190ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std_menu_items(&foo->ctrl_handler, &foo_ctrl_ops,
191ff768f59SMauro Carvalho Chehab			V4L2_CID_TEST_PATTERN, ARRAY_SIZE(test_pattern) - 1, 0,
192ff768f59SMauro Carvalho Chehab			0, test_pattern);
193ff768f59SMauro Carvalho Chehab	...
194ff768f59SMauro Carvalho Chehab	if (foo->ctrl_handler.error) {
195ff768f59SMauro Carvalho Chehab		int err = foo->ctrl_handler.error;
196ff768f59SMauro Carvalho Chehab
197ff768f59SMauro Carvalho Chehab		v4l2_ctrl_handler_free(&foo->ctrl_handler);
198ff768f59SMauro Carvalho Chehab		return err;
199ff768f59SMauro Carvalho Chehab	}
200ff768f59SMauro Carvalho Chehab
201ff768f59SMauro Carvalho ChehabThe :c:func:`v4l2_ctrl_new_std` function returns the v4l2_ctrl pointer to
202ff768f59SMauro Carvalho Chehabthe new control, but if you do not need to access the pointer outside the
203ff768f59SMauro Carvalho Chehabcontrol ops, then there is no need to store it.
204ff768f59SMauro Carvalho Chehab
205ff768f59SMauro Carvalho ChehabThe :c:func:`v4l2_ctrl_new_std` function will fill in most fields based on
206ff768f59SMauro Carvalho Chehabthe control ID except for the min, max, step and default values. These are
207ff768f59SMauro Carvalho Chehabpassed in the last four arguments. These values are driver specific while
208ff768f59SMauro Carvalho Chehabcontrol attributes like type, name, flags are all global. The control's
209ff768f59SMauro Carvalho Chehabcurrent value will be set to the default value.
210ff768f59SMauro Carvalho Chehab
211ff768f59SMauro Carvalho ChehabThe :c:func:`v4l2_ctrl_new_std_menu` function is very similar but it is
212ff768f59SMauro Carvalho Chehabused for menu controls. There is no min argument since that is always 0 for
213ff768f59SMauro Carvalho Chehabmenu controls, and instead of a step there is a skip_mask argument: if bit
214ff768f59SMauro Carvalho ChehabX is 1, then menu item X is skipped.
215ff768f59SMauro Carvalho Chehab
216ff768f59SMauro Carvalho ChehabThe :c:func:`v4l2_ctrl_new_int_menu` function creates a new standard
217ff768f59SMauro Carvalho Chehabinteger menu control with driver-specific items in the menu. It differs
218ff768f59SMauro Carvalho Chehabfrom v4l2_ctrl_new_std_menu in that it doesn't have the mask argument and
219ff768f59SMauro Carvalho Chehabtakes as the last argument an array of signed 64-bit integers that form an
220ff768f59SMauro Carvalho Chehabexact menu item list.
221ff768f59SMauro Carvalho Chehab
222ff768f59SMauro Carvalho ChehabThe :c:func:`v4l2_ctrl_new_std_menu_items` function is very similar to
223ff768f59SMauro Carvalho Chehabv4l2_ctrl_new_std_menu but takes an extra parameter qmenu, which is the
224ff768f59SMauro Carvalho Chehabdriver specific menu for an otherwise standard menu control. A good example
225ff768f59SMauro Carvalho Chehabfor this control is the test pattern control for capture/display/sensors
226ff768f59SMauro Carvalho Chehabdevices that have the capability to generate test patterns. These test
227ff768f59SMauro Carvalho Chehabpatterns are hardware specific, so the contents of the menu will vary from
228ff768f59SMauro Carvalho Chehabdevice to device.
229ff768f59SMauro Carvalho Chehab
230ff768f59SMauro Carvalho ChehabNote that if something fails, the function will return NULL or an error and
231ff768f59SMauro Carvalho Chehabset ctrl_handler->error to the error code. If ctrl_handler->error was already
232ff768f59SMauro Carvalho Chehabset, then it will just return and do nothing. This is also true for
233ff768f59SMauro Carvalho Chehabv4l2_ctrl_handler_init if it cannot allocate the internal data structure.
234ff768f59SMauro Carvalho Chehab
235ff768f59SMauro Carvalho ChehabThis makes it easy to init the handler and just add all controls and only check
236ff768f59SMauro Carvalho Chehabthe error code at the end. Saves a lot of repetitive error checking.
237ff768f59SMauro Carvalho Chehab
238ff768f59SMauro Carvalho ChehabIt is recommended to add controls in ascending control ID order: it will be
239ff768f59SMauro Carvalho Chehaba bit faster that way.
240ff768f59SMauro Carvalho Chehab
241ff768f59SMauro Carvalho Chehab3) Optionally force initial control setup:
242ff768f59SMauro Carvalho Chehab
243ff768f59SMauro Carvalho Chehab.. code-block:: c
244ff768f59SMauro Carvalho Chehab
245ff768f59SMauro Carvalho Chehab	v4l2_ctrl_handler_setup(&foo->ctrl_handler);
246ff768f59SMauro Carvalho Chehab
247ff768f59SMauro Carvalho ChehabThis will call s_ctrl for all controls unconditionally. Effectively this
248ff768f59SMauro Carvalho Chehabinitializes the hardware to the default control values. It is recommended
249ff768f59SMauro Carvalho Chehabthat you do this as this ensures that both the internal data structures and
250ff768f59SMauro Carvalho Chehabthe hardware are in sync.
251ff768f59SMauro Carvalho Chehab
252ff768f59SMauro Carvalho Chehab4) Finally: implement the :c:type:`v4l2_ctrl_ops`
253ff768f59SMauro Carvalho Chehab
254ff768f59SMauro Carvalho Chehab.. code-block:: c
255ff768f59SMauro Carvalho Chehab
256ff768f59SMauro Carvalho Chehab	static const struct v4l2_ctrl_ops foo_ctrl_ops = {
257ff768f59SMauro Carvalho Chehab		.s_ctrl = foo_s_ctrl,
258ff768f59SMauro Carvalho Chehab	};
259ff768f59SMauro Carvalho Chehab
260ff768f59SMauro Carvalho ChehabUsually all you need is s_ctrl:
261ff768f59SMauro Carvalho Chehab
262ff768f59SMauro Carvalho Chehab.. code-block:: c
263ff768f59SMauro Carvalho Chehab
264ff768f59SMauro Carvalho Chehab	static int foo_s_ctrl(struct v4l2_ctrl *ctrl)
265ff768f59SMauro Carvalho Chehab	{
266ff768f59SMauro Carvalho Chehab		struct foo *state = container_of(ctrl->handler, struct foo, ctrl_handler);
267ff768f59SMauro Carvalho Chehab
268ff768f59SMauro Carvalho Chehab		switch (ctrl->id) {
269ff768f59SMauro Carvalho Chehab		case V4L2_CID_BRIGHTNESS:
270ff768f59SMauro Carvalho Chehab			write_reg(0x123, ctrl->val);
271ff768f59SMauro Carvalho Chehab			break;
272ff768f59SMauro Carvalho Chehab		case V4L2_CID_CONTRAST:
273ff768f59SMauro Carvalho Chehab			write_reg(0x456, ctrl->val);
274ff768f59SMauro Carvalho Chehab			break;
275ff768f59SMauro Carvalho Chehab		}
276ff768f59SMauro Carvalho Chehab		return 0;
277ff768f59SMauro Carvalho Chehab	}
278ff768f59SMauro Carvalho Chehab
279ff768f59SMauro Carvalho ChehabThe control ops are called with the v4l2_ctrl pointer as argument.
280ff768f59SMauro Carvalho ChehabThe new control value has already been validated, so all you need to do is
281ff768f59SMauro Carvalho Chehabto actually update the hardware registers.
282ff768f59SMauro Carvalho Chehab
283ff768f59SMauro Carvalho ChehabYou're done! And this is sufficient for most of the drivers we have. No need
284ff768f59SMauro Carvalho Chehabto do any validation of control values, or implement QUERYCTRL, QUERY_EXT_CTRL
285ff768f59SMauro Carvalho Chehaband QUERYMENU. And G/S_CTRL as well as G/TRY/S_EXT_CTRLS are automatically supported.
286ff768f59SMauro Carvalho Chehab
287ff768f59SMauro Carvalho Chehab
288ff768f59SMauro Carvalho Chehab.. note::
289ff768f59SMauro Carvalho Chehab
290ff768f59SMauro Carvalho Chehab   The remainder sections deal with more advanced controls topics and scenarios.
291ff768f59SMauro Carvalho Chehab   In practice the basic usage as described above is sufficient for most drivers.
292ff768f59SMauro Carvalho Chehab
293ff768f59SMauro Carvalho Chehab
294ff768f59SMauro Carvalho ChehabInheriting Sub-device Controls
295ff768f59SMauro Carvalho Chehab------------------------------
296ff768f59SMauro Carvalho Chehab
297ff768f59SMauro Carvalho ChehabWhen a sub-device is registered with a V4L2 driver by calling
298ff768f59SMauro Carvalho Chehabv4l2_device_register_subdev() and the ctrl_handler fields of both v4l2_subdev
299ff768f59SMauro Carvalho Chehaband v4l2_device are set, then the controls of the subdev will become
300ff768f59SMauro Carvalho Chehabautomatically available in the V4L2 driver as well. If the subdev driver
301ff768f59SMauro Carvalho Chehabcontains controls that already exist in the V4L2 driver, then those will be
302ff768f59SMauro Carvalho Chehabskipped (so a V4L2 driver can always override a subdev control).
303ff768f59SMauro Carvalho Chehab
304ff768f59SMauro Carvalho ChehabWhat happens here is that v4l2_device_register_subdev() calls
305ff768f59SMauro Carvalho Chehabv4l2_ctrl_add_handler() adding the controls of the subdev to the controls
306ff768f59SMauro Carvalho Chehabof v4l2_device.
307ff768f59SMauro Carvalho Chehab
308ff768f59SMauro Carvalho Chehab
309ff768f59SMauro Carvalho ChehabAccessing Control Values
310ff768f59SMauro Carvalho Chehab------------------------
311ff768f59SMauro Carvalho Chehab
312ff768f59SMauro Carvalho ChehabThe following union is used inside the control framework to access control
313ff768f59SMauro Carvalho Chehabvalues:
314ff768f59SMauro Carvalho Chehab
315ff768f59SMauro Carvalho Chehab.. code-block:: c
316ff768f59SMauro Carvalho Chehab
317ff768f59SMauro Carvalho Chehab	union v4l2_ctrl_ptr {
318ff768f59SMauro Carvalho Chehab		s32 *p_s32;
319ff768f59SMauro Carvalho Chehab		s64 *p_s64;
320ff768f59SMauro Carvalho Chehab		char *p_char;
321ff768f59SMauro Carvalho Chehab		void *p;
322ff768f59SMauro Carvalho Chehab	};
323ff768f59SMauro Carvalho Chehab
324ff768f59SMauro Carvalho ChehabThe v4l2_ctrl struct contains these fields that can be used to access both
325ff768f59SMauro Carvalho Chehabcurrent and new values:
326ff768f59SMauro Carvalho Chehab
327ff768f59SMauro Carvalho Chehab.. code-block:: c
328ff768f59SMauro Carvalho Chehab
329ff768f59SMauro Carvalho Chehab	s32 val;
330ff768f59SMauro Carvalho Chehab	struct {
331ff768f59SMauro Carvalho Chehab		s32 val;
332ff768f59SMauro Carvalho Chehab	} cur;
333ff768f59SMauro Carvalho Chehab
334ff768f59SMauro Carvalho Chehab
335ff768f59SMauro Carvalho Chehab	union v4l2_ctrl_ptr p_new;
336ff768f59SMauro Carvalho Chehab	union v4l2_ctrl_ptr p_cur;
337ff768f59SMauro Carvalho Chehab
338*eeee0dfdSRandy DunlapIf the control has a simple s32 type, then:
339ff768f59SMauro Carvalho Chehab
340ff768f59SMauro Carvalho Chehab.. code-block:: c
341ff768f59SMauro Carvalho Chehab
342ff768f59SMauro Carvalho Chehab	&ctrl->val == ctrl->p_new.p_s32
343ff768f59SMauro Carvalho Chehab	&ctrl->cur.val == ctrl->p_cur.p_s32
344ff768f59SMauro Carvalho Chehab
345ff768f59SMauro Carvalho ChehabFor all other types use ctrl->p_cur.p<something>. Basically the val
346ff768f59SMauro Carvalho Chehaband cur.val fields can be considered an alias since these are used so often.
347ff768f59SMauro Carvalho Chehab
348ff768f59SMauro Carvalho ChehabWithin the control ops you can freely use these. The val and cur.val speak for
349ff768f59SMauro Carvalho Chehabthemselves. The p_char pointers point to character buffers of length
350ff768f59SMauro Carvalho Chehabctrl->maximum + 1, and are always 0-terminated.
351ff768f59SMauro Carvalho Chehab
352*eeee0dfdSRandy DunlapUnless the control is marked volatile the p_cur field points to the
353ff768f59SMauro Carvalho Chehabcurrent cached control value. When you create a new control this value is made
354ff768f59SMauro Carvalho Chehabidentical to the default value. After calling v4l2_ctrl_handler_setup() this
355ff768f59SMauro Carvalho Chehabvalue is passed to the hardware. It is generally a good idea to call this
356ff768f59SMauro Carvalho Chehabfunction.
357ff768f59SMauro Carvalho Chehab
358ff768f59SMauro Carvalho ChehabWhenever a new value is set that new value is automatically cached. This means
359ff768f59SMauro Carvalho Chehabthat most drivers do not need to implement the g_volatile_ctrl() op. The
360ff768f59SMauro Carvalho Chehabexception is for controls that return a volatile register such as a signal
361ff768f59SMauro Carvalho Chehabstrength read-out that changes continuously. In that case you will need to
362ff768f59SMauro Carvalho Chehabimplement g_volatile_ctrl like this:
363ff768f59SMauro Carvalho Chehab
364ff768f59SMauro Carvalho Chehab.. code-block:: c
365ff768f59SMauro Carvalho Chehab
366ff768f59SMauro Carvalho Chehab	static int foo_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
367ff768f59SMauro Carvalho Chehab	{
368ff768f59SMauro Carvalho Chehab		switch (ctrl->id) {
369ff768f59SMauro Carvalho Chehab		case V4L2_CID_BRIGHTNESS:
370ff768f59SMauro Carvalho Chehab			ctrl->val = read_reg(0x123);
371ff768f59SMauro Carvalho Chehab			break;
372ff768f59SMauro Carvalho Chehab		}
373ff768f59SMauro Carvalho Chehab	}
374ff768f59SMauro Carvalho Chehab
375ff768f59SMauro Carvalho ChehabNote that you use the 'new value' union as well in g_volatile_ctrl. In general
376ff768f59SMauro Carvalho Chehabcontrols that need to implement g_volatile_ctrl are read-only controls. If they
377ff768f59SMauro Carvalho Chehabare not, a V4L2_EVENT_CTRL_CH_VALUE will not be generated when the control
378ff768f59SMauro Carvalho Chehabchanges.
379ff768f59SMauro Carvalho Chehab
380ff768f59SMauro Carvalho ChehabTo mark a control as volatile you have to set V4L2_CTRL_FLAG_VOLATILE:
381ff768f59SMauro Carvalho Chehab
382ff768f59SMauro Carvalho Chehab.. code-block:: c
383ff768f59SMauro Carvalho Chehab
384ff768f59SMauro Carvalho Chehab	ctrl = v4l2_ctrl_new_std(&sd->ctrl_handler, ...);
385ff768f59SMauro Carvalho Chehab	if (ctrl)
386ff768f59SMauro Carvalho Chehab		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
387ff768f59SMauro Carvalho Chehab
388ff768f59SMauro Carvalho ChehabFor try/s_ctrl the new values (i.e. as passed by the user) are filled in and
389ff768f59SMauro Carvalho Chehabyou can modify them in try_ctrl or set them in s_ctrl. The 'cur' union
390ff768f59SMauro Carvalho Chehabcontains the current value, which you can use (but not change!) as well.
391ff768f59SMauro Carvalho Chehab
392ff768f59SMauro Carvalho ChehabIf s_ctrl returns 0 (OK), then the control framework will copy the new final
393ff768f59SMauro Carvalho Chehabvalues to the 'cur' union.
394ff768f59SMauro Carvalho Chehab
395ff768f59SMauro Carvalho ChehabWhile in g_volatile/s/try_ctrl you can access the value of all controls owned
396ff768f59SMauro Carvalho Chehabby the same handler since the handler's lock is held. If you need to access
397ff768f59SMauro Carvalho Chehabthe value of controls owned by other handlers, then you have to be very careful
398ff768f59SMauro Carvalho Chehabnot to introduce deadlocks.
399ff768f59SMauro Carvalho Chehab
400ff768f59SMauro Carvalho ChehabOutside of the control ops you have to go through to helper functions to get
401ff768f59SMauro Carvalho Chehabor set a single control value safely in your driver:
402ff768f59SMauro Carvalho Chehab
403ff768f59SMauro Carvalho Chehab.. code-block:: c
404ff768f59SMauro Carvalho Chehab
405ff768f59SMauro Carvalho Chehab	s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
406ff768f59SMauro Carvalho Chehab	int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
407ff768f59SMauro Carvalho Chehab
408ff768f59SMauro Carvalho ChehabThese functions go through the control framework just as VIDIOC_G/S_CTRL ioctls
409ff768f59SMauro Carvalho Chehabdo. Don't use these inside the control ops g_volatile/s/try_ctrl, though, that
410ff768f59SMauro Carvalho Chehabwill result in a deadlock since these helpers lock the handler as well.
411ff768f59SMauro Carvalho Chehab
412ff768f59SMauro Carvalho ChehabYou can also take the handler lock yourself:
413ff768f59SMauro Carvalho Chehab
414ff768f59SMauro Carvalho Chehab.. code-block:: c
415ff768f59SMauro Carvalho Chehab
416ff768f59SMauro Carvalho Chehab	mutex_lock(&state->ctrl_handler.lock);
417ff768f59SMauro Carvalho Chehab	pr_info("String value is '%s'\n", ctrl1->p_cur.p_char);
418ff768f59SMauro Carvalho Chehab	pr_info("Integer value is '%s'\n", ctrl2->cur.val);
419ff768f59SMauro Carvalho Chehab	mutex_unlock(&state->ctrl_handler.lock);
420ff768f59SMauro Carvalho Chehab
421ff768f59SMauro Carvalho Chehab
422ff768f59SMauro Carvalho ChehabMenu Controls
423ff768f59SMauro Carvalho Chehab-------------
424ff768f59SMauro Carvalho Chehab
425ff768f59SMauro Carvalho ChehabThe v4l2_ctrl struct contains this union:
426ff768f59SMauro Carvalho Chehab
427ff768f59SMauro Carvalho Chehab.. code-block:: c
428ff768f59SMauro Carvalho Chehab
429ff768f59SMauro Carvalho Chehab	union {
430ff768f59SMauro Carvalho Chehab		u32 step;
431ff768f59SMauro Carvalho Chehab		u32 menu_skip_mask;
432ff768f59SMauro Carvalho Chehab	};
433ff768f59SMauro Carvalho Chehab
434ff768f59SMauro Carvalho ChehabFor menu controls menu_skip_mask is used. What it does is that it allows you
435ff768f59SMauro Carvalho Chehabto easily exclude certain menu items. This is used in the VIDIOC_QUERYMENU
436ff768f59SMauro Carvalho Chehabimplementation where you can return -EINVAL if a certain menu item is not
437ff768f59SMauro Carvalho Chehabpresent. Note that VIDIOC_QUERYCTRL always returns a step value of 1 for
438ff768f59SMauro Carvalho Chehabmenu controls.
439ff768f59SMauro Carvalho Chehab
440ff768f59SMauro Carvalho ChehabA good example is the MPEG Audio Layer II Bitrate menu control where the
441ff768f59SMauro Carvalho Chehabmenu is a list of standardized possible bitrates. But in practice hardware
442ff768f59SMauro Carvalho Chehabimplementations will only support a subset of those. By setting the skip
443ff768f59SMauro Carvalho Chehabmask you can tell the framework which menu items should be skipped. Setting
444ff768f59SMauro Carvalho Chehabit to 0 means that all menu items are supported.
445ff768f59SMauro Carvalho Chehab
446ff768f59SMauro Carvalho ChehabYou set this mask either through the v4l2_ctrl_config struct for a custom
447ff768f59SMauro Carvalho Chehabcontrol, or by calling v4l2_ctrl_new_std_menu().
448ff768f59SMauro Carvalho Chehab
449ff768f59SMauro Carvalho Chehab
450ff768f59SMauro Carvalho ChehabCustom Controls
451ff768f59SMauro Carvalho Chehab---------------
452ff768f59SMauro Carvalho Chehab
453ff768f59SMauro Carvalho ChehabDriver specific controls can be created using v4l2_ctrl_new_custom():
454ff768f59SMauro Carvalho Chehab
455ff768f59SMauro Carvalho Chehab.. code-block:: c
456ff768f59SMauro Carvalho Chehab
457ff768f59SMauro Carvalho Chehab	static const struct v4l2_ctrl_config ctrl_filter = {
458ff768f59SMauro Carvalho Chehab		.ops = &ctrl_custom_ops,
459ff768f59SMauro Carvalho Chehab		.id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
460ff768f59SMauro Carvalho Chehab		.name = "Spatial Filter",
461ff768f59SMauro Carvalho Chehab		.type = V4L2_CTRL_TYPE_INTEGER,
462ff768f59SMauro Carvalho Chehab		.flags = V4L2_CTRL_FLAG_SLIDER,
463ff768f59SMauro Carvalho Chehab		.max = 15,
464ff768f59SMauro Carvalho Chehab		.step = 1,
465ff768f59SMauro Carvalho Chehab	};
466ff768f59SMauro Carvalho Chehab
467ff768f59SMauro Carvalho Chehab	ctrl = v4l2_ctrl_new_custom(&foo->ctrl_handler, &ctrl_filter, NULL);
468ff768f59SMauro Carvalho Chehab
469ff768f59SMauro Carvalho ChehabThe last argument is the priv pointer which can be set to driver-specific
470ff768f59SMauro Carvalho Chehabprivate data.
471ff768f59SMauro Carvalho Chehab
472ff768f59SMauro Carvalho ChehabThe v4l2_ctrl_config struct also has a field to set the is_private flag.
473ff768f59SMauro Carvalho Chehab
474ff768f59SMauro Carvalho ChehabIf the name field is not set, then the framework will assume this is a standard
475ff768f59SMauro Carvalho Chehabcontrol and will fill in the name, type and flags fields accordingly.
476ff768f59SMauro Carvalho Chehab
477ff768f59SMauro Carvalho Chehab
478ff768f59SMauro Carvalho ChehabActive and Grabbed Controls
479ff768f59SMauro Carvalho Chehab---------------------------
480ff768f59SMauro Carvalho Chehab
481ff768f59SMauro Carvalho ChehabIf you get more complex relationships between controls, then you may have to
482ff768f59SMauro Carvalho Chehabactivate and deactivate controls. For example, if the Chroma AGC control is
483ff768f59SMauro Carvalho Chehabon, then the Chroma Gain control is inactive. That is, you may set it, but
484ff768f59SMauro Carvalho Chehabthe value will not be used by the hardware as long as the automatic gain
485ff768f59SMauro Carvalho Chehabcontrol is on. Typically user interfaces can disable such input fields.
486ff768f59SMauro Carvalho Chehab
487ff768f59SMauro Carvalho ChehabYou can set the 'active' status using v4l2_ctrl_activate(). By default all
488ff768f59SMauro Carvalho Chehabcontrols are active. Note that the framework does not check for this flag.
489ff768f59SMauro Carvalho ChehabIt is meant purely for GUIs. The function is typically called from within
490ff768f59SMauro Carvalho Chehabs_ctrl.
491ff768f59SMauro Carvalho Chehab
492ff768f59SMauro Carvalho ChehabThe other flag is the 'grabbed' flag. A grabbed control means that you cannot
493ff768f59SMauro Carvalho Chehabchange it because it is in use by some resource. Typical examples are MPEG
494ff768f59SMauro Carvalho Chehabbitrate controls that cannot be changed while capturing is in progress.
495ff768f59SMauro Carvalho Chehab
496ff768f59SMauro Carvalho ChehabIf a control is set to 'grabbed' using v4l2_ctrl_grab(), then the framework
497ff768f59SMauro Carvalho Chehabwill return -EBUSY if an attempt is made to set this control. The
498ff768f59SMauro Carvalho Chehabv4l2_ctrl_grab() function is typically called from the driver when it
499ff768f59SMauro Carvalho Chehabstarts or stops streaming.
500ff768f59SMauro Carvalho Chehab
501ff768f59SMauro Carvalho Chehab
502ff768f59SMauro Carvalho ChehabControl Clusters
503ff768f59SMauro Carvalho Chehab----------------
504ff768f59SMauro Carvalho Chehab
505ff768f59SMauro Carvalho ChehabBy default all controls are independent from the others. But in more
506ff768f59SMauro Carvalho Chehabcomplex scenarios you can get dependencies from one control to another.
507ff768f59SMauro Carvalho ChehabIn that case you need to 'cluster' them:
508ff768f59SMauro Carvalho Chehab
509ff768f59SMauro Carvalho Chehab.. code-block:: c
510ff768f59SMauro Carvalho Chehab
511ff768f59SMauro Carvalho Chehab	struct foo {
512ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl_handler ctrl_handler;
513ff768f59SMauro Carvalho Chehab	#define AUDIO_CL_VOLUME (0)
514ff768f59SMauro Carvalho Chehab	#define AUDIO_CL_MUTE   (1)
515ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl *audio_cluster[2];
516ff768f59SMauro Carvalho Chehab		...
517ff768f59SMauro Carvalho Chehab	};
518ff768f59SMauro Carvalho Chehab
519ff768f59SMauro Carvalho Chehab	state->audio_cluster[AUDIO_CL_VOLUME] =
520ff768f59SMauro Carvalho Chehab		v4l2_ctrl_new_std(&state->ctrl_handler, ...);
521ff768f59SMauro Carvalho Chehab	state->audio_cluster[AUDIO_CL_MUTE] =
522ff768f59SMauro Carvalho Chehab		v4l2_ctrl_new_std(&state->ctrl_handler, ...);
523ff768f59SMauro Carvalho Chehab	v4l2_ctrl_cluster(ARRAY_SIZE(state->audio_cluster), state->audio_cluster);
524ff768f59SMauro Carvalho Chehab
525ff768f59SMauro Carvalho ChehabFrom now on whenever one or more of the controls belonging to the same
526ff768f59SMauro Carvalho Chehabcluster is set (or 'gotten', or 'tried'), only the control ops of the first
527ff768f59SMauro Carvalho Chehabcontrol ('volume' in this example) is called. You effectively create a new
528ff768f59SMauro Carvalho Chehabcomposite control. Similar to how a 'struct' works in C.
529ff768f59SMauro Carvalho Chehab
530ff768f59SMauro Carvalho ChehabSo when s_ctrl is called with V4L2_CID_AUDIO_VOLUME as argument, you should set
531ff768f59SMauro Carvalho Chehaball two controls belonging to the audio_cluster:
532ff768f59SMauro Carvalho Chehab
533ff768f59SMauro Carvalho Chehab.. code-block:: c
534ff768f59SMauro Carvalho Chehab
535ff768f59SMauro Carvalho Chehab	static int foo_s_ctrl(struct v4l2_ctrl *ctrl)
536ff768f59SMauro Carvalho Chehab	{
537ff768f59SMauro Carvalho Chehab		struct foo *state = container_of(ctrl->handler, struct foo, ctrl_handler);
538ff768f59SMauro Carvalho Chehab
539ff768f59SMauro Carvalho Chehab		switch (ctrl->id) {
540ff768f59SMauro Carvalho Chehab		case V4L2_CID_AUDIO_VOLUME: {
541ff768f59SMauro Carvalho Chehab			struct v4l2_ctrl *mute = ctrl->cluster[AUDIO_CL_MUTE];
542ff768f59SMauro Carvalho Chehab
543ff768f59SMauro Carvalho Chehab			write_reg(0x123, mute->val ? 0 : ctrl->val);
544ff768f59SMauro Carvalho Chehab			break;
545ff768f59SMauro Carvalho Chehab		}
546ff768f59SMauro Carvalho Chehab		case V4L2_CID_CONTRAST:
547ff768f59SMauro Carvalho Chehab			write_reg(0x456, ctrl->val);
548ff768f59SMauro Carvalho Chehab			break;
549ff768f59SMauro Carvalho Chehab		}
550ff768f59SMauro Carvalho Chehab		return 0;
551ff768f59SMauro Carvalho Chehab	}
552ff768f59SMauro Carvalho Chehab
553ff768f59SMauro Carvalho ChehabIn the example above the following are equivalent for the VOLUME case:
554ff768f59SMauro Carvalho Chehab
555ff768f59SMauro Carvalho Chehab.. code-block:: c
556ff768f59SMauro Carvalho Chehab
557ff768f59SMauro Carvalho Chehab	ctrl == ctrl->cluster[AUDIO_CL_VOLUME] == state->audio_cluster[AUDIO_CL_VOLUME]
558ff768f59SMauro Carvalho Chehab	ctrl->cluster[AUDIO_CL_MUTE] == state->audio_cluster[AUDIO_CL_MUTE]
559ff768f59SMauro Carvalho Chehab
560ff768f59SMauro Carvalho ChehabIn practice using cluster arrays like this becomes very tiresome. So instead
561ff768f59SMauro Carvalho Chehabthe following equivalent method is used:
562ff768f59SMauro Carvalho Chehab
563ff768f59SMauro Carvalho Chehab.. code-block:: c
564ff768f59SMauro Carvalho Chehab
565ff768f59SMauro Carvalho Chehab	struct {
566ff768f59SMauro Carvalho Chehab		/* audio cluster */
567ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl *volume;
568ff768f59SMauro Carvalho Chehab		struct v4l2_ctrl *mute;
569ff768f59SMauro Carvalho Chehab	};
570ff768f59SMauro Carvalho Chehab
571ff768f59SMauro Carvalho ChehabThe anonymous struct is used to clearly 'cluster' these two control pointers,
572ff768f59SMauro Carvalho Chehabbut it serves no other purpose. The effect is the same as creating an
573ff768f59SMauro Carvalho Chehabarray with two control pointers. So you can just do:
574ff768f59SMauro Carvalho Chehab
575ff768f59SMauro Carvalho Chehab.. code-block:: c
576ff768f59SMauro Carvalho Chehab
577ff768f59SMauro Carvalho Chehab	state->volume = v4l2_ctrl_new_std(&state->ctrl_handler, ...);
578ff768f59SMauro Carvalho Chehab	state->mute = v4l2_ctrl_new_std(&state->ctrl_handler, ...);
579ff768f59SMauro Carvalho Chehab	v4l2_ctrl_cluster(2, &state->volume);
580ff768f59SMauro Carvalho Chehab
581ff768f59SMauro Carvalho ChehabAnd in foo_s_ctrl you can use these pointers directly: state->mute->val.
582ff768f59SMauro Carvalho Chehab
583ff768f59SMauro Carvalho ChehabNote that controls in a cluster may be NULL. For example, if for some
584ff768f59SMauro Carvalho Chehabreason mute was never added (because the hardware doesn't support that
585ff768f59SMauro Carvalho Chehabparticular feature), then mute will be NULL. So in that case we have a
586ff768f59SMauro Carvalho Chehabcluster of 2 controls, of which only 1 is actually instantiated. The
587ff768f59SMauro Carvalho Chehabonly restriction is that the first control of the cluster must always be
588ff768f59SMauro Carvalho Chehabpresent, since that is the 'master' control of the cluster. The master
589ff768f59SMauro Carvalho Chehabcontrol is the one that identifies the cluster and that provides the
590ff768f59SMauro Carvalho Chehabpointer to the v4l2_ctrl_ops struct that is used for that cluster.
591ff768f59SMauro Carvalho Chehab
592ff768f59SMauro Carvalho ChehabObviously, all controls in the cluster array must be initialized to either
593ff768f59SMauro Carvalho Chehaba valid control or to NULL.
594ff768f59SMauro Carvalho Chehab
595ff768f59SMauro Carvalho ChehabIn rare cases you might want to know which controls of a cluster actually
596ff768f59SMauro Carvalho Chehabwere set explicitly by the user. For this you can check the 'is_new' flag of
597ff768f59SMauro Carvalho Chehabeach control. For example, in the case of a volume/mute cluster the 'is_new'
598ff768f59SMauro Carvalho Chehabflag of the mute control would be set if the user called VIDIOC_S_CTRL for
599ff768f59SMauro Carvalho Chehabmute only. If the user would call VIDIOC_S_EXT_CTRLS for both mute and volume
600ff768f59SMauro Carvalho Chehabcontrols, then the 'is_new' flag would be 1 for both controls.
601ff768f59SMauro Carvalho Chehab
602ff768f59SMauro Carvalho ChehabThe 'is_new' flag is always 1 when called from v4l2_ctrl_handler_setup().
603ff768f59SMauro Carvalho Chehab
604ff768f59SMauro Carvalho Chehab
605ff768f59SMauro Carvalho ChehabHandling autogain/gain-type Controls with Auto Clusters
606ff768f59SMauro Carvalho Chehab-------------------------------------------------------
607ff768f59SMauro Carvalho Chehab
608ff768f59SMauro Carvalho ChehabA common type of control cluster is one that handles 'auto-foo/foo'-type
609ff768f59SMauro Carvalho Chehabcontrols. Typical examples are autogain/gain, autoexposure/exposure,
610ff768f59SMauro Carvalho Chehabautowhitebalance/red balance/blue balance. In all cases you have one control
611ff768f59SMauro Carvalho Chehabthat determines whether another control is handled automatically by the hardware,
612ff768f59SMauro Carvalho Chehabor whether it is under manual control from the user.
613ff768f59SMauro Carvalho Chehab
614ff768f59SMauro Carvalho ChehabIf the cluster is in automatic mode, then the manual controls should be
615ff768f59SMauro Carvalho Chehabmarked inactive and volatile. When the volatile controls are read the
616ff768f59SMauro Carvalho Chehabg_volatile_ctrl operation should return the value that the hardware's automatic
617ff768f59SMauro Carvalho Chehabmode set up automatically.
618ff768f59SMauro Carvalho Chehab
619ff768f59SMauro Carvalho ChehabIf the cluster is put in manual mode, then the manual controls should become
620ff768f59SMauro Carvalho Chehabactive again and the volatile flag is cleared (so g_volatile_ctrl is no longer
621ff768f59SMauro Carvalho Chehabcalled while in manual mode). In addition just before switching to manual mode
622ff768f59SMauro Carvalho Chehabthe current values as determined by the auto mode are copied as the new manual
623ff768f59SMauro Carvalho Chehabvalues.
624ff768f59SMauro Carvalho Chehab
625ff768f59SMauro Carvalho ChehabFinally the V4L2_CTRL_FLAG_UPDATE should be set for the auto control since
626ff768f59SMauro Carvalho Chehabchanging that control affects the control flags of the manual controls.
627ff768f59SMauro Carvalho Chehab
628ff768f59SMauro Carvalho ChehabIn order to simplify this a special variation of v4l2_ctrl_cluster was
629ff768f59SMauro Carvalho Chehabintroduced:
630ff768f59SMauro Carvalho Chehab
631ff768f59SMauro Carvalho Chehab.. code-block:: c
632ff768f59SMauro Carvalho Chehab
633ff768f59SMauro Carvalho Chehab	void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
634ff768f59SMauro Carvalho Chehab				    u8 manual_val, bool set_volatile);
635ff768f59SMauro Carvalho Chehab
636ff768f59SMauro Carvalho ChehabThe first two arguments are identical to v4l2_ctrl_cluster. The third argument
637ff768f59SMauro Carvalho Chehabtells the framework which value switches the cluster into manual mode. The
638ff768f59SMauro Carvalho Chehablast argument will optionally set V4L2_CTRL_FLAG_VOLATILE for the non-auto controls.
639ff768f59SMauro Carvalho ChehabIf it is false, then the manual controls are never volatile. You would typically
640ff768f59SMauro Carvalho Chehabuse that if the hardware does not give you the option to read back to values as
641ff768f59SMauro Carvalho Chehabdetermined by the auto mode (e.g. if autogain is on, the hardware doesn't allow
642ff768f59SMauro Carvalho Chehabyou to obtain the current gain value).
643ff768f59SMauro Carvalho Chehab
644ff768f59SMauro Carvalho ChehabThe first control of the cluster is assumed to be the 'auto' control.
645ff768f59SMauro Carvalho Chehab
646ff768f59SMauro Carvalho ChehabUsing this function will ensure that you don't need to handle all the complex
647ff768f59SMauro Carvalho Chehabflag and volatile handling.
648ff768f59SMauro Carvalho Chehab
649ff768f59SMauro Carvalho Chehab
650ff768f59SMauro Carvalho ChehabVIDIOC_LOG_STATUS Support
651ff768f59SMauro Carvalho Chehab-------------------------
652ff768f59SMauro Carvalho Chehab
653ff768f59SMauro Carvalho ChehabThis ioctl allow you to dump the current status of a driver to the kernel log.
654ff768f59SMauro Carvalho ChehabThe v4l2_ctrl_handler_log_status(ctrl_handler, prefix) can be used to dump the
655ff768f59SMauro Carvalho Chehabvalue of the controls owned by the given handler to the log. You can supply a
656ff768f59SMauro Carvalho Chehabprefix as well. If the prefix didn't end with a space, then ': ' will be added
657ff768f59SMauro Carvalho Chehabfor you.
658ff768f59SMauro Carvalho Chehab
659ff768f59SMauro Carvalho Chehab
660ff768f59SMauro Carvalho ChehabDifferent Handlers for Different Video Nodes
661ff768f59SMauro Carvalho Chehab--------------------------------------------
662ff768f59SMauro Carvalho Chehab
663ff768f59SMauro Carvalho ChehabUsually the V4L2 driver has just one control handler that is global for
664ff768f59SMauro Carvalho Chehaball video nodes. But you can also specify different control handlers for
665ff768f59SMauro Carvalho Chehabdifferent video nodes. You can do that by manually setting the ctrl_handler
666ff768f59SMauro Carvalho Chehabfield of struct video_device.
667ff768f59SMauro Carvalho Chehab
668ff768f59SMauro Carvalho ChehabThat is no problem if there are no subdevs involved but if there are, then
669ff768f59SMauro Carvalho Chehabyou need to block the automatic merging of subdev controls to the global
670ff768f59SMauro Carvalho Chehabcontrol handler. You do that by simply setting the ctrl_handler field in
671ff768f59SMauro Carvalho Chehabstruct v4l2_device to NULL. Now v4l2_device_register_subdev() will no longer
672ff768f59SMauro Carvalho Chehabmerge subdev controls.
673ff768f59SMauro Carvalho Chehab
674ff768f59SMauro Carvalho ChehabAfter each subdev was added, you will then have to call v4l2_ctrl_add_handler
675ff768f59SMauro Carvalho Chehabmanually to add the subdev's control handler (sd->ctrl_handler) to the desired
676ff768f59SMauro Carvalho Chehabcontrol handler. This control handler may be specific to the video_device or
677ff768f59SMauro Carvalho Chehabfor a subset of video_device's. For example: the radio device nodes only have
678ff768f59SMauro Carvalho Chehabaudio controls, while the video and vbi device nodes share the same control
679ff768f59SMauro Carvalho Chehabhandler for the audio and video controls.
680ff768f59SMauro Carvalho Chehab
681ff768f59SMauro Carvalho ChehabIf you want to have one handler (e.g. for a radio device node) have a subset
682ff768f59SMauro Carvalho Chehabof another handler (e.g. for a video device node), then you should first add
683ff768f59SMauro Carvalho Chehabthe controls to the first handler, add the other controls to the second
684ff768f59SMauro Carvalho Chehabhandler and finally add the first handler to the second. For example:
685ff768f59SMauro Carvalho Chehab
686ff768f59SMauro Carvalho Chehab.. code-block:: c
687ff768f59SMauro Carvalho Chehab
688ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_VOLUME, ...);
689ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_MUTE, ...);
690ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_BRIGHTNESS, ...);
691ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_CONTRAST, ...);
692ff768f59SMauro Carvalho Chehab	v4l2_ctrl_add_handler(&video_ctrl_handler, &radio_ctrl_handler, NULL);
693ff768f59SMauro Carvalho Chehab
694ff768f59SMauro Carvalho ChehabThe last argument to v4l2_ctrl_add_handler() is a filter function that allows
695ff768f59SMauro Carvalho Chehabyou to filter which controls will be added. Set it to NULL if you want to add
696ff768f59SMauro Carvalho Chehaball controls.
697ff768f59SMauro Carvalho Chehab
698ff768f59SMauro Carvalho ChehabOr you can add specific controls to a handler:
699ff768f59SMauro Carvalho Chehab
700ff768f59SMauro Carvalho Chehab.. code-block:: c
701ff768f59SMauro Carvalho Chehab
702ff768f59SMauro Carvalho Chehab	volume = v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_AUDIO_VOLUME, ...);
703ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_BRIGHTNESS, ...);
704ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_CONTRAST, ...);
705ff768f59SMauro Carvalho Chehab
706ff768f59SMauro Carvalho ChehabWhat you should not do is make two identical controls for two handlers.
707ff768f59SMauro Carvalho ChehabFor example:
708ff768f59SMauro Carvalho Chehab
709ff768f59SMauro Carvalho Chehab.. code-block:: c
710ff768f59SMauro Carvalho Chehab
711ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_MUTE, ...);
712ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_AUDIO_MUTE, ...);
713ff768f59SMauro Carvalho Chehab
714ff768f59SMauro Carvalho ChehabThis would be bad since muting the radio would not change the video mute
715ff768f59SMauro Carvalho Chehabcontrol. The rule is to have one control for each hardware 'knob' that you
716ff768f59SMauro Carvalho Chehabcan twiddle.
717ff768f59SMauro Carvalho Chehab
718ff768f59SMauro Carvalho Chehab
719ff768f59SMauro Carvalho ChehabFinding Controls
720ff768f59SMauro Carvalho Chehab----------------
721ff768f59SMauro Carvalho Chehab
722ff768f59SMauro Carvalho ChehabNormally you have created the controls yourself and you can store the struct
723ff768f59SMauro Carvalho Chehabv4l2_ctrl pointer into your own struct.
724ff768f59SMauro Carvalho Chehab
725ff768f59SMauro Carvalho ChehabBut sometimes you need to find a control from another handler that you do
726ff768f59SMauro Carvalho Chehabnot own. For example, if you have to find a volume control from a subdev.
727ff768f59SMauro Carvalho Chehab
728ff768f59SMauro Carvalho ChehabYou can do that by calling v4l2_ctrl_find:
729ff768f59SMauro Carvalho Chehab
730ff768f59SMauro Carvalho Chehab.. code-block:: c
731ff768f59SMauro Carvalho Chehab
732ff768f59SMauro Carvalho Chehab	struct v4l2_ctrl *volume;
733ff768f59SMauro Carvalho Chehab
734ff768f59SMauro Carvalho Chehab	volume = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_AUDIO_VOLUME);
735ff768f59SMauro Carvalho Chehab
736ff768f59SMauro Carvalho ChehabSince v4l2_ctrl_find will lock the handler you have to be careful where you
737ff768f59SMauro Carvalho Chehabuse it. For example, this is not a good idea:
738ff768f59SMauro Carvalho Chehab
739ff768f59SMauro Carvalho Chehab.. code-block:: c
740ff768f59SMauro Carvalho Chehab
741ff768f59SMauro Carvalho Chehab	struct v4l2_ctrl_handler ctrl_handler;
742ff768f59SMauro Carvalho Chehab
743ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&ctrl_handler, &video_ops, V4L2_CID_BRIGHTNESS, ...);
744ff768f59SMauro Carvalho Chehab	v4l2_ctrl_new_std(&ctrl_handler, &video_ops, V4L2_CID_CONTRAST, ...);
745ff768f59SMauro Carvalho Chehab
746ff768f59SMauro Carvalho Chehab...and in video_ops.s_ctrl:
747ff768f59SMauro Carvalho Chehab
748ff768f59SMauro Carvalho Chehab.. code-block:: c
749ff768f59SMauro Carvalho Chehab
750ff768f59SMauro Carvalho Chehab	case V4L2_CID_BRIGHTNESS:
751ff768f59SMauro Carvalho Chehab		contrast = v4l2_find_ctrl(&ctrl_handler, V4L2_CID_CONTRAST);
752ff768f59SMauro Carvalho Chehab		...
753ff768f59SMauro Carvalho Chehab
754ff768f59SMauro Carvalho ChehabWhen s_ctrl is called by the framework the ctrl_handler.lock is already taken, so
755ff768f59SMauro Carvalho Chehabattempting to find another control from the same handler will deadlock.
756ff768f59SMauro Carvalho Chehab
757ff768f59SMauro Carvalho ChehabIt is recommended not to use this function from inside the control ops.
758ff768f59SMauro Carvalho Chehab
759ff768f59SMauro Carvalho Chehab
760ff768f59SMauro Carvalho ChehabPreventing Controls inheritance
761ff768f59SMauro Carvalho Chehab-------------------------------
762ff768f59SMauro Carvalho Chehab
763ff768f59SMauro Carvalho ChehabWhen one control handler is added to another using v4l2_ctrl_add_handler, then
764ff768f59SMauro Carvalho Chehabby default all controls from one are merged to the other. But a subdev might
765ff768f59SMauro Carvalho Chehabhave low-level controls that make sense for some advanced embedded system, but
766ff768f59SMauro Carvalho Chehabnot when it is used in consumer-level hardware. In that case you want to keep
767ff768f59SMauro Carvalho Chehabthose low-level controls local to the subdev. You can do this by simply
768ff768f59SMauro Carvalho Chehabsetting the 'is_private' flag of the control to 1:
769ff768f59SMauro Carvalho Chehab
770ff768f59SMauro Carvalho Chehab.. code-block:: c
771ff768f59SMauro Carvalho Chehab
772ff768f59SMauro Carvalho Chehab	static const struct v4l2_ctrl_config ctrl_private = {
773ff768f59SMauro Carvalho Chehab		.ops = &ctrl_custom_ops,
774ff768f59SMauro Carvalho Chehab		.id = V4L2_CID_...,
775ff768f59SMauro Carvalho Chehab		.name = "Some Private Control",
776ff768f59SMauro Carvalho Chehab		.type = V4L2_CTRL_TYPE_INTEGER,
777ff768f59SMauro Carvalho Chehab		.max = 15,
778ff768f59SMauro Carvalho Chehab		.step = 1,
779ff768f59SMauro Carvalho Chehab		.is_private = 1,
780ff768f59SMauro Carvalho Chehab	};
781ff768f59SMauro Carvalho Chehab
782ff768f59SMauro Carvalho Chehab	ctrl = v4l2_ctrl_new_custom(&foo->ctrl_handler, &ctrl_private, NULL);
783ff768f59SMauro Carvalho Chehab
784ff768f59SMauro Carvalho ChehabThese controls will now be skipped when v4l2_ctrl_add_handler is called.
785ff768f59SMauro Carvalho Chehab
786ff768f59SMauro Carvalho Chehab
787ff768f59SMauro Carvalho ChehabV4L2_CTRL_TYPE_CTRL_CLASS Controls
788ff768f59SMauro Carvalho Chehab----------------------------------
789ff768f59SMauro Carvalho Chehab
790ff768f59SMauro Carvalho ChehabControls of this type can be used by GUIs to get the name of the control class.
791ff768f59SMauro Carvalho ChehabA fully featured GUI can make a dialog with multiple tabs with each tab
792ff768f59SMauro Carvalho Chehabcontaining the controls belonging to a particular control class. The name of
793ff768f59SMauro Carvalho Chehabeach tab can be found by querying a special control with ID <control class | 1>.
794ff768f59SMauro Carvalho Chehab
795ff768f59SMauro Carvalho ChehabDrivers do not have to care about this. The framework will automatically add
796ff768f59SMauro Carvalho Chehaba control of this type whenever the first control belonging to a new control
797ff768f59SMauro Carvalho Chehabclass is added.
798ff768f59SMauro Carvalho Chehab
799ff768f59SMauro Carvalho Chehab
800ff768f59SMauro Carvalho ChehabAdding Notify Callbacks
801ff768f59SMauro Carvalho Chehab-----------------------
802ff768f59SMauro Carvalho Chehab
803ff768f59SMauro Carvalho ChehabSometimes the platform or bridge driver needs to be notified when a control
804ff768f59SMauro Carvalho Chehabfrom a sub-device driver changes. You can set a notify callback by calling
805ff768f59SMauro Carvalho Chehabthis function:
806ff768f59SMauro Carvalho Chehab
807ff768f59SMauro Carvalho Chehab.. code-block:: c
808ff768f59SMauro Carvalho Chehab
809ff768f59SMauro Carvalho Chehab	void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl,
810ff768f59SMauro Carvalho Chehab		void (*notify)(struct v4l2_ctrl *ctrl, void *priv), void *priv);
811ff768f59SMauro Carvalho Chehab
812ff768f59SMauro Carvalho ChehabWhenever the give control changes value the notify callback will be called
813ff768f59SMauro Carvalho Chehabwith a pointer to the control and the priv pointer that was passed with
814ff768f59SMauro Carvalho Chehabv4l2_ctrl_notify. Note that the control's handler lock is held when the
815ff768f59SMauro Carvalho Chehabnotify function is called.
816ff768f59SMauro Carvalho Chehab
817ff768f59SMauro Carvalho ChehabThere can be only one notify function per control handler. Any attempt
818ff768f59SMauro Carvalho Chehabto set another notify function will cause a WARN_ON.
819ff768f59SMauro Carvalho Chehab
820ff768f59SMauro Carvalho Chehabv4l2_ctrl functions and data structures
821ff768f59SMauro Carvalho Chehab---------------------------------------
822ff768f59SMauro Carvalho Chehab
823ff768f59SMauro Carvalho Chehab.. kernel-doc:: include/media/v4l2-ctrls.h
824