1.. Permission is granted to copy, distribute and/or modify this
2.. document under the terms of the GNU Free Documentation License,
3.. Version 1.1 or any later version published by the Free Software
4.. Foundation, with no Invariant Sections, no Front-Cover Texts
5.. and no Back-Cover Texts. A copy of the license is included at
6.. Documentation/userspace-api/media/fdl-appendix.rst.
7..
8.. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections
9
10.. _VIDIOC_G_EXT_CTRLS:
11
12******************************************************************
13ioctl VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS
14******************************************************************
15
16Name
17====
18
19VIDIOC_G_EXT_CTRLS - VIDIOC_S_EXT_CTRLS - VIDIOC_TRY_EXT_CTRLS - Get or set the value of several controls, try control values
20
21
22Synopsis
23========
24
25.. c:function:: int ioctl( int fd, VIDIOC_G_EXT_CTRLS, struct v4l2_ext_controls *argp )
26    :name: VIDIOC_G_EXT_CTRLS
27
28
29.. c:function:: int ioctl( int fd, VIDIOC_S_EXT_CTRLS, struct v4l2_ext_controls *argp )
30    :name: VIDIOC_S_EXT_CTRLS
31
32
33.. c:function:: int ioctl( int fd, VIDIOC_TRY_EXT_CTRLS, struct v4l2_ext_controls *argp )
34    :name: VIDIOC_TRY_EXT_CTRLS
35
36
37Arguments
38=========
39
40``fd``
41    File descriptor returned by :ref:`open() <func-open>`.
42
43``argp``
44    Pointer to struct :c:type:`v4l2_ext_controls`.
45
46
47Description
48===========
49
50These ioctls allow the caller to get or set multiple controls
51atomically. Control IDs are grouped into control classes (see
52:ref:`ctrl-class`) and all controls in the control array must belong
53to the same control class.
54
55Applications must always fill in the ``count``, ``which``, ``controls``
56and ``reserved`` fields of struct
57:c:type:`v4l2_ext_controls`, and initialize the
58struct :c:type:`v4l2_ext_control` array pointed to
59by the ``controls`` fields.
60
61To get the current value of a set of controls applications initialize
62the ``id``, ``size`` and ``reserved2`` fields of each struct
63:c:type:`v4l2_ext_control` and call the
64:ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` ioctl. String controls controls must also set the
65``string`` field. Controls of compound types
66(``V4L2_CTRL_FLAG_HAS_PAYLOAD`` is set) must set the ``ptr`` field.
67
68If the ``size`` is too small to receive the control result (only
69relevant for pointer-type controls like strings), then the driver will
70set ``size`` to a valid value and return an ``ENOSPC`` error code. You
71should re-allocate the memory to this new size and try again. For the
72string type it is possible that the same issue occurs again if the
73string has grown in the meantime. It is recommended to call
74:ref:`VIDIOC_QUERYCTRL` first and use
75``maximum``\ +1 as the new ``size`` value. It is guaranteed that that is
76sufficient memory.
77
78N-dimensional arrays are set and retrieved row-by-row. You cannot set a
79partial array, all elements have to be set or retrieved. The total size
80is calculated as ``elems`` * ``elem_size``. These values can be obtained
81by calling :ref:`VIDIOC_QUERY_EXT_CTRL <VIDIOC_QUERYCTRL>`.
82
83To change the value of a set of controls applications initialize the
84``id``, ``size``, ``reserved2`` and ``value/value64/string/ptr`` fields
85of each struct :c:type:`v4l2_ext_control` and call
86the :ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` ioctl. The controls will only be set if *all*
87control values are valid.
88
89To check if a set of controls have correct values applications
90initialize the ``id``, ``size``, ``reserved2`` and
91``value/value64/string/ptr`` fields of each struct
92:c:type:`v4l2_ext_control` and call the
93:ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` ioctl. It is up to the driver whether wrong
94values are automatically adjusted to a valid value or if an error is
95returned.
96
97When the ``id`` or ``which`` is invalid drivers return an ``EINVAL`` error
98code. When the value is out of bounds drivers can choose to take the
99closest valid value or return an ``ERANGE`` error code, whatever seems more
100appropriate. In the first case the new value is set in struct
101:c:type:`v4l2_ext_control`. If the new control value
102is inappropriate (e.g. the given menu index is not supported by the menu
103control), then this will also result in an ``EINVAL`` error code error.
104
105If ``request_fd`` is set to a not-yet-queued :ref:`request <media-request-api>`
106file descriptor and ``which`` is set to ``V4L2_CTRL_WHICH_REQUEST_VAL``,
107then the controls are not applied immediately when calling
108:ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`, but instead are applied by
109the driver for the buffer associated with the same request.
110If the device does not support requests, then ``EACCES`` will be returned.
111If requests are supported but an invalid request file descriptor is given,
112then ``EINVAL`` will be returned.
113
114An attempt to call :ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` for a
115request that has already been queued will result in an ``EBUSY`` error.
116
117If ``request_fd`` is specified and ``which`` is set to
118``V4L2_CTRL_WHICH_REQUEST_VAL`` during a call to
119:ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`, then it will return the
120values of the controls at the time of request completion.
121If the request is not yet completed, then this will result in an
122``EACCES`` error.
123
124The driver will only set/get these controls if all control values are
125correct. This prevents the situation where only some of the controls
126were set/get. Only low-level errors (e. g. a failed i2c command) can
127still cause this situation.
128
129
130.. tabularcolumns:: |p{1.2cm}|p{3.0cm}|p{1.5cm}|p{11.8cm}|
131
132.. c:type:: v4l2_ext_control
133
134.. cssclass: longtable
135
136.. flat-table:: struct v4l2_ext_control
137    :header-rows:  0
138    :stub-columns: 0
139    :widths:       1 1 2
140
141    * - __u32
142      - ``id``
143      - Identifies the control, set by the application.
144    * - __u32
145      - ``size``
146      - The total size in bytes of the payload of this control. This is
147	normally 0, but for pointer controls this should be set to the
148	size of the memory containing the payload, or that will receive
149	the payload. If :ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` finds that this value is
150	less than is required to store the payload result, then it is set
151	to a value large enough to store the payload result and ``ENOSPC`` is
152	returned.
153
154	.. note::
155
156	   For string controls, this ``size`` field should
157	   not be confused with the length of the string. This field refers
158	   to the size of the memory that contains the string. The actual
159	   *length* of the string may well be much smaller.
160    * - __u32
161      - ``reserved2``\ [1]
162      - Reserved for future extensions. Drivers and applications must set
163	the array to zero.
164    * - union {
165      - (anonymous)
166    * - __s32
167      - ``value``
168      - New value or current value. Valid if this control is not of type
169	``V4L2_CTRL_TYPE_INTEGER64`` and ``V4L2_CTRL_FLAG_HAS_PAYLOAD`` is
170	not set.
171    * - __s64
172      - ``value64``
173      - New value or current value. Valid if this control is of type
174	``V4L2_CTRL_TYPE_INTEGER64`` and ``V4L2_CTRL_FLAG_HAS_PAYLOAD`` is
175	not set.
176    * - char *
177      - ``string``
178      - A pointer to a string. Valid if this control is of type
179	``V4L2_CTRL_TYPE_STRING``.
180    * - __u8 *
181      - ``p_u8``
182      - A pointer to a matrix control of unsigned 8-bit values. Valid if
183	this control is of type ``V4L2_CTRL_TYPE_U8``.
184    * - __u16 *
185      - ``p_u16``
186      - A pointer to a matrix control of unsigned 16-bit values. Valid if
187	this control is of type ``V4L2_CTRL_TYPE_U16``.
188    * - __u32 *
189      - ``p_u32``
190      - A pointer to a matrix control of unsigned 32-bit values. Valid if
191	this control is of type ``V4L2_CTRL_TYPE_U32``.
192    * - :c:type:`v4l2_area` *
193      - ``p_area``
194      - A pointer to a struct :c:type:`v4l2_area`. Valid if this control is
195        of type ``V4L2_CTRL_TYPE_AREA``.
196    * - void *
197      - ``ptr``
198      - A pointer to a compound type which can be an N-dimensional array
199	and/or a compound type (the control's type is >=
200	``V4L2_CTRL_COMPOUND_TYPES``). Valid if
201	``V4L2_CTRL_FLAG_HAS_PAYLOAD`` is set for this control.
202    * - }
203      -
204
205
206.. tabularcolumns:: |p{4.0cm}|p{2.2cm}|p{2.1cm}|p{8.2cm}|
207
208.. c:type:: v4l2_ext_controls
209
210.. cssclass:: longtable
211
212.. flat-table:: struct v4l2_ext_controls
213    :header-rows:  0
214    :stub-columns: 0
215    :widths:       1 1 2
216
217    * - union {
218      - (anonymous)
219    * - __u32
220      - ``ctrl_class``
221      - The control class to which all controls belong, see
222	:ref:`ctrl-class`. Drivers that use a kernel framework for
223	handling controls will also accept a value of 0 here, meaning that
224	the controls can belong to any control class. Whether drivers
225	support this can be tested by setting ``ctrl_class`` to 0 and
226	calling :ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` with a ``count`` of 0. If that
227	succeeds, then the driver supports this feature.
228    * - __u32
229      - ``which``
230      - Which value of the control to get/set/try.
231	``V4L2_CTRL_WHICH_CUR_VAL`` will return the current value of the
232	control, ``V4L2_CTRL_WHICH_DEF_VAL`` will return the default
233	value of the control and ``V4L2_CTRL_WHICH_REQUEST_VAL`` indicates that
234	these controls have to be retrieved from a request or tried/set for
235	a request. In the latter case the ``request_fd`` field contains the
236	file descriptor of the request that should be used. If the device
237	does not support requests, then ``EACCES`` will be returned.
238
239	.. note::
240
241	   When using ``V4L2_CTRL_WHICH_DEF_VAL`` be aware that you can only
242	   get the default value of the control, you cannot set or try it.
243
244	For backwards compatibility you can also use a control class here
245	(see :ref:`ctrl-class`). In that case all controls have to
246	belong to that control class. This usage is deprecated, instead
247	just use ``V4L2_CTRL_WHICH_CUR_VAL``. There are some very old
248	drivers that do not yet support ``V4L2_CTRL_WHICH_CUR_VAL`` and
249	that require a control class here. You can test for such drivers
250	by setting ctrl_class to ``V4L2_CTRL_WHICH_CUR_VAL`` and calling
251	VIDIOC_TRY_EXT_CTRLS with a count of 0. If that fails, then the
252	driver does not support ``V4L2_CTRL_WHICH_CUR_VAL``.
253    * - }
254      -
255    * - __u32
256      - ``count``
257      - The number of controls in the controls array. May also be zero.
258    * - __u32
259      - ``error_idx``
260      - Set by the driver in case of an error. If the error is associated
261	with a particular control, then ``error_idx`` is set to the index
262	of that control. If the error is not related to a specific
263	control, or the validation step failed (see below), then
264	``error_idx`` is set to ``count``. The value is undefined if the
265	ioctl returned 0 (success).
266
267	Before controls are read from/written to hardware a validation
268	step takes place: this checks if all controls in the list are
269	valid controls, if no attempt is made to write to a read-only
270	control or read from a write-only control, and any other up-front
271	checks that can be done without accessing the hardware. The exact
272	validations done during this step are driver dependent since some
273	checks might require hardware access for some devices, thus making
274	it impossible to do those checks up-front. However, drivers should
275	make a best-effort to do as many up-front checks as possible.
276
277	This check is done to avoid leaving the hardware in an
278	inconsistent state due to easy-to-avoid problems. But it leads to
279	another problem: the application needs to know whether an error
280	came from the validation step (meaning that the hardware was not
281	touched) or from an error during the actual reading from/writing
282	to hardware.
283
284	The, in hindsight quite poor, solution for that is to set
285	``error_idx`` to ``count`` if the validation failed. This has the
286	unfortunate side-effect that it is not possible to see which
287	control failed the validation. If the validation was successful
288	and the error happened while accessing the hardware, then
289	``error_idx`` is less than ``count`` and only the controls up to
290	``error_idx-1`` were read or written correctly, and the state of
291	the remaining controls is undefined.
292
293	Since :ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` does not access hardware there is
294	also no need to handle the validation step in this special way, so
295	``error_idx`` will just be set to the control that failed the
296	validation step instead of to ``count``. This means that if
297	:ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` fails with ``error_idx`` set to ``count``,
298	then you can call :ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` to try to discover the
299	actual control that failed the validation step. Unfortunately,
300	there is no ``TRY`` equivalent for :ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`.
301    * - __s32
302      - ``request_fd``
303      - File descriptor of the request to be used by this operation. Only
304	valid if ``which`` is set to ``V4L2_CTRL_WHICH_REQUEST_VAL``.
305	If the device does not support requests, then ``EACCES`` will be returned.
306	If requests are supported but an invalid request file descriptor is
307	given, then ``EINVAL`` will be returned.
308    * - __u32
309      - ``reserved``\ [1]
310      - Reserved for future extensions.
311
312	Drivers and applications must set the array to zero.
313    * - struct :c:type:`v4l2_ext_control` *
314      - ``controls``
315      - Pointer to an array of ``count`` v4l2_ext_control structures.
316
317	Ignored if ``count`` equals zero.
318
319
320.. tabularcolumns:: |p{6.6cm}|p{2.2cm}|p{8.7cm}|
321
322.. _ctrl-class:
323
324.. flat-table:: Control classes
325    :header-rows:  0
326    :stub-columns: 0
327    :widths:       3 1 4
328
329    * - ``V4L2_CTRL_CLASS_USER``
330      - 0x980000
331      - The class containing user controls. These controls are described
332	in :ref:`control`. All controls that can be set using the
333	:ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>` and
334	:ref:`VIDIOC_G_CTRL <VIDIOC_G_CTRL>` ioctl belong to this
335	class.
336    * - ``V4L2_CTRL_CLASS_MPEG``
337      - 0x990000
338      - The class containing MPEG compression controls. These controls are
339	described in :ref:`mpeg-controls`.
340    * - ``V4L2_CTRL_CLASS_CAMERA``
341      - 0x9a0000
342      - The class containing camera controls. These controls are described
343	in :ref:`camera-controls`.
344    * - ``V4L2_CTRL_CLASS_FM_TX``
345      - 0x9b0000
346      - The class containing FM Transmitter (FM TX) controls. These
347	controls are described in :ref:`fm-tx-controls`.
348    * - ``V4L2_CTRL_CLASS_FLASH``
349      - 0x9c0000
350      - The class containing flash device controls. These controls are
351	described in :ref:`flash-controls`.
352    * - ``V4L2_CTRL_CLASS_JPEG``
353      - 0x9d0000
354      - The class containing JPEG compression controls. These controls are
355	described in :ref:`jpeg-controls`.
356    * - ``V4L2_CTRL_CLASS_IMAGE_SOURCE``
357      - 0x9e0000
358      - The class containing image source controls. These controls are
359	described in :ref:`image-source-controls`.
360    * - ``V4L2_CTRL_CLASS_IMAGE_PROC``
361      - 0x9f0000
362      - The class containing image processing controls. These controls are
363	described in :ref:`image-process-controls`.
364    * - ``V4L2_CTRL_CLASS_FM_RX``
365      - 0xa10000
366      - The class containing FM Receiver (FM RX) controls. These controls
367	are described in :ref:`fm-rx-controls`.
368    * - ``V4L2_CTRL_CLASS_RF_TUNER``
369      - 0xa20000
370      - The class containing RF tuner controls. These controls are
371	described in :ref:`rf-tuner-controls`.
372
373
374Return Value
375============
376
377On success 0 is returned, on error -1 and the ``errno`` variable is set
378appropriately. The generic error codes are described at the
379:ref:`Generic Error Codes <gen-errors>` chapter.
380
381EINVAL
382    The struct :c:type:`v4l2_ext_control` ``id`` is
383    invalid, or the struct :c:type:`v4l2_ext_controls`
384    ``which`` is invalid, or the struct
385    :c:type:`v4l2_ext_control` ``value`` was
386    inappropriate (e.g. the given menu index is not supported by the
387    driver), or the ``which`` field was set to ``V4L2_CTRL_WHICH_REQUEST_VAL``
388    but the given ``request_fd`` was invalid or ``V4L2_CTRL_WHICH_REQUEST_VAL``
389    is not supported by the kernel.
390    This error code is also returned by the
391    :ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` and :ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` ioctls if two or
392    more control values are in conflict.
393
394ERANGE
395    The struct :c:type:`v4l2_ext_control` ``value``
396    is out of bounds.
397
398EBUSY
399    The control is temporarily not changeable, possibly because another
400    applications took over control of the device function this control
401    belongs to, or (if the ``which`` field was set to
402    ``V4L2_CTRL_WHICH_REQUEST_VAL``) the request was queued but not yet
403    completed.
404
405ENOSPC
406    The space reserved for the control's payload is insufficient. The
407    field ``size`` is set to a value that is enough to store the payload
408    and this error code is returned.
409
410EACCES
411    Attempt to try or set a read-only control, or to get a write-only
412    control, or to get a control from a request that has not yet been
413    completed.
414
415    Or the ``which`` field was set to ``V4L2_CTRL_WHICH_REQUEST_VAL`` but the
416    device does not support requests.
417