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