Merge tag 'v5.9' into next
[linux-2.6-microblaze.git] / Documentation / userspace-api / media / v4l / dev-subdev.rst
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 .. _subdev:
11
12 ********************
13 Sub-device Interface
14 ********************
15
16 The complex nature of V4L2 devices, where hardware is often made of
17 several integrated circuits that need to interact with each other in a
18 controlled way, leads to complex V4L2 drivers. The drivers usually
19 reflect the hardware model in software, and model the different hardware
20 components as software blocks called sub-devices.
21
22 V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver
23 implements the media device API, they will automatically inherit from
24 media entities. Applications will be able to enumerate the sub-devices
25 and discover the hardware topology using the media entities, pads and
26 links enumeration API.
27
28 In addition to make sub-devices discoverable, drivers can also choose to
29 make them directly configurable by applications. When both the
30 sub-device driver and the V4L2 device driver support this, sub-devices
31 will feature a character device node on which ioctls can be called to
32
33 -  query, read and write sub-devices controls
34
35 -  subscribe and unsubscribe to events and retrieve them
36
37 -  negotiate image formats on individual pads
38
39 Sub-device character device nodes, conventionally named
40 ``/dev/v4l-subdev*``, use major number 81.
41
42 Drivers may opt to limit the sub-device character devices to only expose
43 operations that do not modify the device state. In such a case the sub-devices
44 are referred to as ``read-only`` in the rest of this documentation, and the
45 related restrictions are documented in individual ioctls.
46
47
48 Controls
49 ========
50
51 Most V4L2 controls are implemented by sub-device hardware. Drivers
52 usually merge all controls and expose them through video device nodes.
53 Applications can control all sub-devices through a single interface.
54
55 Complex devices sometimes implement the same control in different pieces
56 of hardware. This situation is common in embedded platforms, where both
57 sensors and image processing hardware implement identical functions,
58 such as contrast adjustment, white balance or faulty pixels correction.
59 As the V4L2 controls API doesn't support several identical controls in a
60 single device, all but one of the identical controls are hidden.
61
62 Applications can access those hidden controls through the sub-device
63 node with the V4L2 control API described in :ref:`control`. The ioctls
64 behave identically as when issued on V4L2 device nodes, with the
65 exception that they deal only with controls implemented in the
66 sub-device.
67
68 Depending on the driver, those controls might also be exposed through
69 one (or several) V4L2 device nodes.
70
71
72 Events
73 ======
74
75 V4L2 sub-devices can notify applications of events as described in
76 :ref:`event`. The API behaves identically as when used on V4L2 device
77 nodes, with the exception that it only deals with events generated by
78 the sub-device. Depending on the driver, those events might also be
79 reported on one (or several) V4L2 device nodes.
80
81
82 .. _pad-level-formats:
83
84 Pad-level Formats
85 =================
86
87 .. warning::
88
89     Pad-level formats are only applicable to very complex devices that
90     need to expose low-level format configuration to user space. Generic
91     V4L2 applications do *not* need to use the API described in this
92     section.
93
94 .. note::
95
96     For the purpose of this section, the term *format* means the
97     combination of media bus data format, frame width and frame height.
98
99 Image formats are typically negotiated on video capture and output
100 devices using the format and
101 :ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is
102 responsible for configuring every block in the video pipeline according
103 to the requested format at the pipeline input and/or output.
104
105 For complex devices, such as often found in embedded systems, identical
106 image sizes at the output of a pipeline can be achieved using different
107 hardware configurations. One such example is shown on
108 :ref:`pipeline-scaling`, where image scaling can be performed on both
109 the video sensor and the host image processing hardware.
110
111
112 .. _pipeline-scaling:
113
114 .. kernel-figure:: pipeline.dot
115     :alt:   pipeline.dot
116     :align: center
117
118     Image Format Negotiation on Pipelines
119
120     High quality and high speed pipeline configuration
121
122
123
124 The sensor scaler is usually of less quality than the host scaler, but
125 scaling on the sensor is required to achieve higher frame rates.
126 Depending on the use case (quality vs. speed), the pipeline must be
127 configured differently. Applications need to configure the formats at
128 every point in the pipeline explicitly.
129
130 Drivers that implement the :ref:`media API <media-controller-intro>`
131 can expose pad-level image format configuration to applications. When
132 they do, applications can use the
133 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and
134 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls. to
135 negotiate formats on a per-pad basis.
136
137 Applications are responsible for configuring coherent parameters on the
138 whole pipeline and making sure that connected pads have compatible
139 formats. The pipeline is checked for formats mismatch at
140 :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error
141 code is then returned if the configuration is invalid.
142
143 Pad-level image format configuration support can be tested by calling
144 the :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad
145 0. If the driver returns an ``EINVAL`` error code pad-level format
146 configuration is not supported by the sub-device.
147
148
149 Format Negotiation
150 ------------------
151
152 Acceptable formats on pads can (and usually do) depend on a number of
153 external parameters, such as formats on other pads, active links, or
154 even controls. Finding a combination of formats on all pads in a video
155 pipeline, acceptable to both application and driver, can't rely on
156 formats enumeration only. A format negotiation mechanism is required.
157
158 Central to the format negotiation mechanism are the get/set format
159 operations. When called with the ``which`` argument set to
160 :ref:`V4L2_SUBDEV_FORMAT_TRY <VIDIOC_SUBDEV_G_FMT>`, the
161 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and
162 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls operate on
163 a set of formats parameters that are not connected to the hardware
164 configuration. Modifying those 'try' formats leaves the device state
165 untouched (this applies to both the software state stored in the driver
166 and the hardware state stored in the device itself).
167
168 While not kept as part of the device state, try formats are stored in
169 the sub-device file handles. A
170 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return
171 the last try format set *on the same sub-device file handle*. Several
172 applications querying the same sub-device at the same time will thus not
173 interact with each other.
174
175 To find out whether a particular format is supported by the device,
176 applications use the
177 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers
178 verify and, if needed, change the requested ``format`` based on device
179 requirements and return the possibly modified value. Applications can
180 then choose to try a different format or accept the returned value and
181 continue.
182
183 Formats returned by the driver during a negotiation iteration are
184 guaranteed to be supported by the device. In particular, drivers
185 guarantee that a returned format will not be further changed if passed
186 to an :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` call as-is
187 (as long as external parameters, such as formats on other pads or links'
188 configuration are not changed).
189
190 Drivers automatically propagate formats inside sub-devices. When a try
191 or active format is set on a pad, corresponding formats on other pads of
192 the same sub-device can be modified by the driver. Drivers are free to
193 modify formats as required by the device. However, they should comply
194 with the following rules when possible:
195
196 -  Formats should be propagated from sink pads to source pads. Modifying
197    a format on a source pad should not modify the format on any sink
198    pad.
199
200 -  Sub-devices that scale frames using variable scaling factors should
201    reset the scale factors to default values when sink pads formats are
202    modified. If the 1:1 scaling ratio is supported, this means that
203    source pads formats should be reset to the sink pads formats.
204
205 Formats are not propagated across links, as that would involve
206 propagating them from one sub-device file handle to another.
207 Applications must then take care to configure both ends of every link
208 explicitly with compatible formats. Identical formats on the two ends of
209 a link are guaranteed to be compatible. Drivers are free to accept
210 different formats matching device requirements as being compatible.
211
212 :ref:`sample-pipeline-config` shows a sample configuration sequence
213 for the pipeline described in :ref:`pipeline-scaling` (table columns
214 list entity names and pad numbers).
215
216
217 .. raw:: latex
218
219     \scriptsize
220
221 .. tabularcolumns:: |p{2.0cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|
222
223 .. _sample-pipeline-config:
224
225 .. flat-table:: Sample Pipeline Configuration
226     :header-rows:  1
227     :stub-columns: 0
228     :widths: 5 5 5 5 5 5 5
229
230     * -
231       - Sensor/0
232
233         format
234       - Frontend/0
235
236         format
237       - Frontend/1
238
239         format
240       - Scaler/0
241
242         format
243       - Scaler/0
244
245         compose selection rectangle
246       - Scaler/1
247
248         format
249     * - Initial state
250       - 2048x1536
251
252         SGRBG8_1X8
253       - (default)
254       - (default)
255       - (default)
256       - (default)
257       - (default)
258     * - Configure frontend sink format
259       - 2048x1536
260
261         SGRBG8_1X8
262       - *2048x1536*
263
264         *SGRBG8_1X8*
265       - *2046x1534*
266
267         *SGRBG8_1X8*
268       - (default)
269       - (default)
270       - (default)
271     * - Configure scaler sink format
272       - 2048x1536
273
274         SGRBG8_1X8
275       - 2048x1536
276
277         SGRBG8_1X8
278       - 2046x1534
279
280         SGRBG8_1X8
281       - *2046x1534*
282
283         *SGRBG8_1X8*
284       - *0,0/2046x1534*
285       - *2046x1534*
286
287         *SGRBG8_1X8*
288     * - Configure scaler sink compose selection
289       - 2048x1536
290
291         SGRBG8_1X8
292       - 2048x1536
293
294         SGRBG8_1X8
295       - 2046x1534
296
297         SGRBG8_1X8
298       - 2046x1534
299
300         SGRBG8_1X8
301       - *0,0/1280x960*
302       - *1280x960*
303
304         *SGRBG8_1X8*
305
306 .. raw:: latex
307
308     \normalsize
309
310 1. Initial state. The sensor source pad format is set to its native 3MP
311    size and V4L2_MBUS_FMT_SGRBG8_1X8 media bus code. Formats on the
312    host frontend and scaler sink and source pads have the default
313    values, as well as the compose rectangle on the scaler's sink pad.
314
315 2. The application configures the frontend sink pad format's size to
316    2048x1536 and its media bus code to V4L2_MBUS_FMT_SGRBG_1X8. The
317    driver propagates the format to the frontend source pad.
318
319 3. The application configures the scaler sink pad format's size to
320    2046x1534 and the media bus code to V4L2_MBUS_FMT_SGRBG_1X8 to
321    match the frontend source size and media bus code. The media bus code
322    on the sink pad is set to V4L2_MBUS_FMT_SGRBG_1X8. The driver
323    propagates the size to the compose selection rectangle on the
324    scaler's sink pad, and the format to the scaler source pad.
325
326 4. The application configures the size of the compose selection
327    rectangle of the scaler's sink pad 1280x960. The driver propagates
328    the size to the scaler's source pad format.
329
330 When satisfied with the try results, applications can set the active
331 formats by setting the ``which`` argument to
332 ``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try
333 formats by drivers. To avoid modifying the hardware state during format
334 negotiation, applications should negotiate try formats first and then
335 modify the active settings using the try formats returned during the
336 last negotiation iteration. This guarantees that the active format will
337 be applied as-is by the driver without being modified.
338
339
340 .. _v4l2-subdev-selections:
341
342 Selections: cropping, scaling and composition
343 ---------------------------------------------
344
345 Many sub-devices support cropping frames on their input or output pads
346 (or possible even on both). Cropping is used to select the area of
347 interest in an image, typically on an image sensor or a video decoder.
348 It can also be used as part of digital zoom implementations to select
349 the area of the image that will be scaled up.
350
351 Crop settings are defined by a crop rectangle and represented in a
352 struct :c:type:`v4l2_rect` by the coordinates of the top
353 left corner and the rectangle size. Both the coordinates and sizes are
354 expressed in pixels.
355
356 As for pad formats, drivers store try and active rectangles for the
357 selection targets :ref:`v4l2-selections-common`.
358
359 On sink pads, cropping is applied relative to the current pad format.
360 The pad format represents the image size as received by the sub-device
361 from the previous block in the pipeline, and the crop rectangle
362 represents the sub-image that will be transmitted further inside the
363 sub-device for processing.
364
365 The scaling operation changes the size of the image by scaling it to new
366 dimensions. The scaling ratio isn't specified explicitly, but is implied
367 from the original and scaled image sizes. Both sizes are represented by
368 struct :c:type:`v4l2_rect`.
369
370 Scaling support is optional. When supported by a subdev, the crop
371 rectangle on the subdev's sink pad is scaled to the size configured
372 using the
373 :ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL
374 using ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the
375 subdev supports scaling but not composing, the top and left values are
376 not used and must always be set to zero.
377
378 On source pads, cropping is similar to sink pads, with the exception
379 that the source size from which the cropping is performed, is the
380 COMPOSE rectangle on the sink pad. In both sink and source pads, the
381 crop rectangle must be entirely contained inside the source image size
382 for the crop operation.
383
384 The drivers should always use the closest possible rectangle the user
385 requests on all selection targets, unless specifically told otherwise.
386 ``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round
387 the image size either up or down. :ref:`v4l2-selection-flags`
388
389
390 Types of selection targets
391 --------------------------
392
393
394 Actual targets
395 ^^^^^^^^^^^^^^
396
397 Actual targets (without a postfix) reflect the actual hardware
398 configuration at any point of time. There is a BOUNDS target
399 corresponding to every actual target.
400
401
402 BOUNDS targets
403 ^^^^^^^^^^^^^^
404
405 BOUNDS targets is the smallest rectangle that contains all valid actual
406 rectangles. It may not be possible to set the actual rectangle as large
407 as the BOUNDS rectangle, however. This may be because e.g. a sensor's
408 pixel array is not rectangular but cross-shaped or round. The maximum
409 size may also be smaller than the BOUNDS rectangle.
410
411
412 Order of configuration and format propagation
413 ---------------------------------------------
414
415 Inside subdevs, the order of image processing steps will always be from
416 the sink pad towards the source pad. This is also reflected in the order
417 in which the configuration must be performed by the user: the changes
418 made will be propagated to any subsequent stages. If this behaviour is
419 not desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This
420 flag causes no propagation of the changes are allowed in any
421 circumstances. This may also cause the accessed rectangle to be adjusted
422 by the driver, depending on the properties of the underlying hardware.
423
424 The coordinates to a step always refer to the actual size of the
425 previous step. The exception to this rule is the sink compose
426 rectangle, which refers to the sink compose bounds rectangle --- if it
427 is supported by the hardware.
428
429 1. Sink pad format. The user configures the sink pad format. This format
430    defines the parameters of the image the entity receives through the
431    pad for further processing.
432
433 2. Sink pad actual crop selection. The sink pad crop defines the crop
434    performed to the sink pad format.
435
436 3. Sink pad actual compose selection. The size of the sink pad compose
437    rectangle defines the scaling ratio compared to the size of the sink
438    pad crop rectangle. The location of the compose rectangle specifies
439    the location of the actual sink compose rectangle in the sink compose
440    bounds rectangle.
441
442 4. Source pad actual crop selection. Crop on the source pad defines crop
443    performed to the image in the sink compose bounds rectangle.
444
445 5. Source pad format. The source pad format defines the output pixel
446    format of the subdev, as well as the other parameters with the
447    exception of the image width and height. Width and height are defined
448    by the size of the source pad actual crop selection.
449
450 Accessing any of the above rectangles not supported by the subdev will
451 return ``EINVAL``. Any rectangle referring to a previous unsupported
452 rectangle coordinates will instead refer to the previous supported
453 rectangle. For example, if sink crop is not supported, the compose
454 selection will refer to the sink pad format dimensions instead.
455
456
457 .. _subdev-image-processing-crop:
458
459 .. kernel-figure:: subdev-image-processing-crop.svg
460     :alt:   subdev-image-processing-crop.svg
461     :align: center
462
463     **Figure 4.5. Image processing in subdevs: simple crop example**
464
465 In the above example, the subdev supports cropping on its sink pad. To
466 configure it, the user sets the media bus format on the subdev's sink
467 pad. Now the actual crop rectangle can be set on the sink pad --- the
468 location and size of this rectangle reflect the location and size of a
469 rectangle to be cropped from the sink format. The size of the sink crop
470 rectangle will also be the size of the format of the subdev's source
471 pad.
472
473
474 .. _subdev-image-processing-scaling-multi-source:
475
476 .. kernel-figure:: subdev-image-processing-scaling-multi-source.svg
477     :alt:   subdev-image-processing-scaling-multi-source.svg
478     :align: center
479
480     **Figure 4.6. Image processing in subdevs: scaling with multiple sources**
481
482 In this example, the subdev is capable of first cropping, then scaling
483 and finally cropping for two source pads individually from the resulting
484 scaled image. The location of the scaled image in the cropped image is
485 ignored in sink compose target. Both of the locations of the source crop
486 rectangles refer to the sink scaling rectangle, independently cropping
487 an area at location specified by the source crop rectangle from it.
488
489
490 .. _subdev-image-processing-full:
491
492 .. kernel-figure:: subdev-image-processing-full.svg
493     :alt:    subdev-image-processing-full.svg
494     :align:  center
495
496     **Figure 4.7. Image processing in subdevs: scaling and composition with multiple sinks and sources**
497
498 The subdev driver supports two sink pads and two source pads. The images
499 from both of the sink pads are individually cropped, then scaled and
500 further composed on the composition bounds rectangle. From that, two
501 independent streams are cropped and sent out of the subdev from the
502 source pads.
503
504
505 .. toctree::
506     :maxdepth: 1
507
508     subdev-formats