0001 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
0002
0003 .. _subdev:
0004
0005 ********************
0006 Sub-device Interface
0007 ********************
0008
0009 The complex nature of V4L2 devices, where hardware is often made of
0010 several integrated circuits that need to interact with each other in a
0011 controlled way, leads to complex V4L2 drivers. The drivers usually
0012 reflect the hardware model in software, and model the different hardware
0013 components as software blocks called sub-devices.
0014
0015 V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver
0016 implements the media device API, they will automatically inherit from
0017 media entities. Applications will be able to enumerate the sub-devices
0018 and discover the hardware topology using the media entities, pads and
0019 links enumeration API.
0020
0021 In addition to make sub-devices discoverable, drivers can also choose to
0022 make them directly configurable by applications. When both the
0023 sub-device driver and the V4L2 device driver support this, sub-devices
0024 will feature a character device node on which ioctls can be called to
0025
0026 - query, read and write sub-devices controls
0027
0028 - subscribe and unsubscribe to events and retrieve them
0029
0030 - negotiate image formats on individual pads
0031
0032 Sub-device character device nodes, conventionally named
0033 ``/dev/v4l-subdev*``, use major number 81.
0034
0035 Drivers may opt to limit the sub-device character devices to only expose
0036 operations that do not modify the device state. In such a case the sub-devices
0037 are referred to as ``read-only`` in the rest of this documentation, and the
0038 related restrictions are documented in individual ioctls.
0039
0040
0041 Controls
0042 ========
0043
0044 Most V4L2 controls are implemented by sub-device hardware. Drivers
0045 usually merge all controls and expose them through video device nodes.
0046 Applications can control all sub-devices through a single interface.
0047
0048 Complex devices sometimes implement the same control in different pieces
0049 of hardware. This situation is common in embedded platforms, where both
0050 sensors and image processing hardware implement identical functions,
0051 such as contrast adjustment, white balance or faulty pixels correction.
0052 As the V4L2 controls API doesn't support several identical controls in a
0053 single device, all but one of the identical controls are hidden.
0054
0055 Applications can access those hidden controls through the sub-device
0056 node with the V4L2 control API described in :ref:`control`. The ioctls
0057 behave identically as when issued on V4L2 device nodes, with the
0058 exception that they deal only with controls implemented in the
0059 sub-device.
0060
0061 Depending on the driver, those controls might also be exposed through
0062 one (or several) V4L2 device nodes.
0063
0064
0065 Events
0066 ======
0067
0068 V4L2 sub-devices can notify applications of events as described in
0069 :ref:`event`. The API behaves identically as when used on V4L2 device
0070 nodes, with the exception that it only deals with events generated by
0071 the sub-device. Depending on the driver, those events might also be
0072 reported on one (or several) V4L2 device nodes.
0073
0074
0075 .. _pad-level-formats:
0076
0077 Pad-level Formats
0078 =================
0079
0080 .. warning::
0081
0082 Pad-level formats are only applicable to very complex devices that
0083 need to expose low-level format configuration to user space. Generic
0084 V4L2 applications do *not* need to use the API described in this
0085 section.
0086
0087 .. note::
0088
0089 For the purpose of this section, the term *format* means the
0090 combination of media bus data format, frame width and frame height.
0091
0092 Image formats are typically negotiated on video capture and output
0093 devices using the format and
0094 :ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is
0095 responsible for configuring every block in the video pipeline according
0096 to the requested format at the pipeline input and/or output.
0097
0098 For complex devices, such as often found in embedded systems, identical
0099 image sizes at the output of a pipeline can be achieved using different
0100 hardware configurations. One such example is shown on
0101 :ref:`pipeline-scaling`, where image scaling can be performed on both
0102 the video sensor and the host image processing hardware.
0103
0104
0105 .. _pipeline-scaling:
0106
0107 .. kernel-figure:: pipeline.dot
0108 :alt: pipeline.dot
0109 :align: center
0110
0111 Image Format Negotiation on Pipelines
0112
0113 High quality and high speed pipeline configuration
0114
0115
0116
0117 The sensor scaler is usually of less quality than the host scaler, but
0118 scaling on the sensor is required to achieve higher frame rates.
0119 Depending on the use case (quality vs. speed), the pipeline must be
0120 configured differently. Applications need to configure the formats at
0121 every point in the pipeline explicitly.
0122
0123 Drivers that implement the :ref:`media API <media-controller-intro>`
0124 can expose pad-level image format configuration to applications. When
0125 they do, applications can use the
0126 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and
0127 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls. to
0128 negotiate formats on a per-pad basis.
0129
0130 Applications are responsible for configuring coherent parameters on the
0131 whole pipeline and making sure that connected pads have compatible
0132 formats. The pipeline is checked for formats mismatch at
0133 :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error
0134 code is then returned if the configuration is invalid.
0135
0136 Pad-level image format configuration support can be tested by calling
0137 the :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad
0138 0. If the driver returns an ``EINVAL`` error code pad-level format
0139 configuration is not supported by the sub-device.
0140
0141
0142 Format Negotiation
0143 ------------------
0144
0145 Acceptable formats on pads can (and usually do) depend on a number of
0146 external parameters, such as formats on other pads, active links, or
0147 even controls. Finding a combination of formats on all pads in a video
0148 pipeline, acceptable to both application and driver, can't rely on
0149 formats enumeration only. A format negotiation mechanism is required.
0150
0151 Central to the format negotiation mechanism are the get/set format
0152 operations. When called with the ``which`` argument set to
0153 :ref:`V4L2_SUBDEV_FORMAT_TRY <VIDIOC_SUBDEV_G_FMT>`, the
0154 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and
0155 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls operate on
0156 a set of formats parameters that are not connected to the hardware
0157 configuration. Modifying those 'try' formats leaves the device state
0158 untouched (this applies to both the software state stored in the driver
0159 and the hardware state stored in the device itself).
0160
0161 While not kept as part of the device state, try formats are stored in
0162 the sub-device file handles. A
0163 :ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return
0164 the last try format set *on the same sub-device file handle*. Several
0165 applications querying the same sub-device at the same time will thus not
0166 interact with each other.
0167
0168 To find out whether a particular format is supported by the device,
0169 applications use the
0170 :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers
0171 verify and, if needed, change the requested ``format`` based on device
0172 requirements and return the possibly modified value. Applications can
0173 then choose to try a different format or accept the returned value and
0174 continue.
0175
0176 Formats returned by the driver during a negotiation iteration are
0177 guaranteed to be supported by the device. In particular, drivers
0178 guarantee that a returned format will not be further changed if passed
0179 to an :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` call as-is
0180 (as long as external parameters, such as formats on other pads or links'
0181 configuration are not changed).
0182
0183 Drivers automatically propagate formats inside sub-devices. When a try
0184 or active format is set on a pad, corresponding formats on other pads of
0185 the same sub-device can be modified by the driver. Drivers are free to
0186 modify formats as required by the device. However, they should comply
0187 with the following rules when possible:
0188
0189 - Formats should be propagated from sink pads to source pads. Modifying
0190 a format on a source pad should not modify the format on any sink
0191 pad.
0192
0193 - Sub-devices that scale frames using variable scaling factors should
0194 reset the scale factors to default values when sink pads formats are
0195 modified. If the 1:1 scaling ratio is supported, this means that
0196 source pads formats should be reset to the sink pads formats.
0197
0198 Formats are not propagated across links, as that would involve
0199 propagating them from one sub-device file handle to another.
0200 Applications must then take care to configure both ends of every link
0201 explicitly with compatible formats. Identical formats on the two ends of
0202 a link are guaranteed to be compatible. Drivers are free to accept
0203 different formats matching device requirements as being compatible.
0204
0205 :ref:`sample-pipeline-config` shows a sample configuration sequence
0206 for the pipeline described in :ref:`pipeline-scaling` (table columns
0207 list entity names and pad numbers).
0208
0209
0210 .. raw:: latex
0211
0212 \begingroup
0213 \scriptsize
0214 \setlength{\tabcolsep}{2pt}
0215
0216 .. tabularcolumns:: |p{2.0cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|
0217
0218 .. _sample-pipeline-config:
0219
0220 .. flat-table:: Sample Pipeline Configuration
0221 :header-rows: 1
0222 :stub-columns: 0
0223 :widths: 5 5 5 5 5 5 5
0224
0225 * -
0226 - Sensor/0
0227
0228 format
0229 - Frontend/0
0230
0231 format
0232 - Frontend/1
0233
0234 format
0235 - Scaler/0
0236
0237 format
0238 - Scaler/0
0239
0240 compose selection rectangle
0241 - Scaler/1
0242
0243 format
0244 * - Initial state
0245 - 2048x1536
0246
0247 SGRBG8_1X8
0248 - (default)
0249 - (default)
0250 - (default)
0251 - (default)
0252 - (default)
0253 * - Configure frontend sink format
0254 - 2048x1536
0255
0256 SGRBG8_1X8
0257 - *2048x1536*
0258
0259 *SGRBG8_1X8*
0260 - *2046x1534*
0261
0262 *SGRBG8_1X8*
0263 - (default)
0264 - (default)
0265 - (default)
0266 * - Configure scaler sink format
0267 - 2048x1536
0268
0269 SGRBG8_1X8
0270 - 2048x1536
0271
0272 SGRBG8_1X8
0273 - 2046x1534
0274
0275 SGRBG8_1X8
0276 - *2046x1534*
0277
0278 *SGRBG8_1X8*
0279 - *0,0/2046x1534*
0280 - *2046x1534*
0281
0282 *SGRBG8_1X8*
0283 * - Configure scaler sink compose selection
0284 - 2048x1536
0285
0286 SGRBG8_1X8
0287 - 2048x1536
0288
0289 SGRBG8_1X8
0290 - 2046x1534
0291
0292 SGRBG8_1X8
0293 - 2046x1534
0294
0295 SGRBG8_1X8
0296 - *0,0/1280x960*
0297 - *1280x960*
0298
0299 *SGRBG8_1X8*
0300
0301 .. raw:: latex
0302
0303 \endgroup
0304
0305 1. Initial state. The sensor source pad format is set to its native 3MP
0306 size and V4L2_MBUS_FMT_SGRBG8_1X8 media bus code. Formats on the
0307 host frontend and scaler sink and source pads have the default
0308 values, as well as the compose rectangle on the scaler's sink pad.
0309
0310 2. The application configures the frontend sink pad format's size to
0311 2048x1536 and its media bus code to V4L2_MBUS_FMT_SGRBG_1X8. The
0312 driver propagates the format to the frontend source pad.
0313
0314 3. The application configures the scaler sink pad format's size to
0315 2046x1534 and the media bus code to V4L2_MBUS_FMT_SGRBG_1X8 to
0316 match the frontend source size and media bus code. The media bus code
0317 on the sink pad is set to V4L2_MBUS_FMT_SGRBG_1X8. The driver
0318 propagates the size to the compose selection rectangle on the
0319 scaler's sink pad, and the format to the scaler source pad.
0320
0321 4. The application configures the size of the compose selection
0322 rectangle of the scaler's sink pad 1280x960. The driver propagates
0323 the size to the scaler's source pad format.
0324
0325 When satisfied with the try results, applications can set the active
0326 formats by setting the ``which`` argument to
0327 ``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try
0328 formats by drivers. To avoid modifying the hardware state during format
0329 negotiation, applications should negotiate try formats first and then
0330 modify the active settings using the try formats returned during the
0331 last negotiation iteration. This guarantees that the active format will
0332 be applied as-is by the driver without being modified.
0333
0334
0335 .. _v4l2-subdev-selections:
0336
0337 Selections: cropping, scaling and composition
0338 ---------------------------------------------
0339
0340 Many sub-devices support cropping frames on their input or output pads
0341 (or possible even on both). Cropping is used to select the area of
0342 interest in an image, typically on an image sensor or a video decoder.
0343 It can also be used as part of digital zoom implementations to select
0344 the area of the image that will be scaled up.
0345
0346 Crop settings are defined by a crop rectangle and represented in a
0347 struct :c:type:`v4l2_rect` by the coordinates of the top
0348 left corner and the rectangle size. Both the coordinates and sizes are
0349 expressed in pixels.
0350
0351 As for pad formats, drivers store try and active rectangles for the
0352 selection targets :ref:`v4l2-selections-common`.
0353
0354 On sink pads, cropping is applied relative to the current pad format.
0355 The pad format represents the image size as received by the sub-device
0356 from the previous block in the pipeline, and the crop rectangle
0357 represents the sub-image that will be transmitted further inside the
0358 sub-device for processing.
0359
0360 The scaling operation changes the size of the image by scaling it to new
0361 dimensions. The scaling ratio isn't specified explicitly, but is implied
0362 from the original and scaled image sizes. Both sizes are represented by
0363 struct :c:type:`v4l2_rect`.
0364
0365 Scaling support is optional. When supported by a subdev, the crop
0366 rectangle on the subdev's sink pad is scaled to the size configured
0367 using the
0368 :ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL
0369 using ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the
0370 subdev supports scaling but not composing, the top and left values are
0371 not used and must always be set to zero.
0372
0373 On source pads, cropping is similar to sink pads, with the exception
0374 that the source size from which the cropping is performed, is the
0375 COMPOSE rectangle on the sink pad. In both sink and source pads, the
0376 crop rectangle must be entirely contained inside the source image size
0377 for the crop operation.
0378
0379 The drivers should always use the closest possible rectangle the user
0380 requests on all selection targets, unless specifically told otherwise.
0381 ``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round
0382 the image size either up or down. :ref:`v4l2-selection-flags`
0383
0384
0385 Types of selection targets
0386 --------------------------
0387
0388
0389 Actual targets
0390 ^^^^^^^^^^^^^^
0391
0392 Actual targets (without a postfix) reflect the actual hardware
0393 configuration at any point of time. There is a BOUNDS target
0394 corresponding to every actual target.
0395
0396
0397 BOUNDS targets
0398 ^^^^^^^^^^^^^^
0399
0400 BOUNDS targets is the smallest rectangle that contains all valid actual
0401 rectangles. It may not be possible to set the actual rectangle as large
0402 as the BOUNDS rectangle, however. This may be because e.g. a sensor's
0403 pixel array is not rectangular but cross-shaped or round. The maximum
0404 size may also be smaller than the BOUNDS rectangle.
0405
0406
0407 Order of configuration and format propagation
0408 ---------------------------------------------
0409
0410 Inside subdevs, the order of image processing steps will always be from
0411 the sink pad towards the source pad. This is also reflected in the order
0412 in which the configuration must be performed by the user: the changes
0413 made will be propagated to any subsequent stages. If this behaviour is
0414 not desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This
0415 flag causes no propagation of the changes are allowed in any
0416 circumstances. This may also cause the accessed rectangle to be adjusted
0417 by the driver, depending on the properties of the underlying hardware.
0418
0419 The coordinates to a step always refer to the actual size of the
0420 previous step. The exception to this rule is the sink compose
0421 rectangle, which refers to the sink compose bounds rectangle --- if it
0422 is supported by the hardware.
0423
0424 1. Sink pad format. The user configures the sink pad format. This format
0425 defines the parameters of the image the entity receives through the
0426 pad for further processing.
0427
0428 2. Sink pad actual crop selection. The sink pad crop defines the crop
0429 performed to the sink pad format.
0430
0431 3. Sink pad actual compose selection. The size of the sink pad compose
0432 rectangle defines the scaling ratio compared to the size of the sink
0433 pad crop rectangle. The location of the compose rectangle specifies
0434 the location of the actual sink compose rectangle in the sink compose
0435 bounds rectangle.
0436
0437 4. Source pad actual crop selection. Crop on the source pad defines crop
0438 performed to the image in the sink compose bounds rectangle.
0439
0440 5. Source pad format. The source pad format defines the output pixel
0441 format of the subdev, as well as the other parameters with the
0442 exception of the image width and height. Width and height are defined
0443 by the size of the source pad actual crop selection.
0444
0445 Accessing any of the above rectangles not supported by the subdev will
0446 return ``EINVAL``. Any rectangle referring to a previous unsupported
0447 rectangle coordinates will instead refer to the previous supported
0448 rectangle. For example, if sink crop is not supported, the compose
0449 selection will refer to the sink pad format dimensions instead.
0450
0451
0452 .. _subdev-image-processing-crop:
0453
0454 .. kernel-figure:: subdev-image-processing-crop.svg
0455 :alt: subdev-image-processing-crop.svg
0456 :align: center
0457
0458 **Figure 4.5. Image processing in subdevs: simple crop example**
0459
0460 In the above example, the subdev supports cropping on its sink pad. To
0461 configure it, the user sets the media bus format on the subdev's sink
0462 pad. Now the actual crop rectangle can be set on the sink pad --- the
0463 location and size of this rectangle reflect the location and size of a
0464 rectangle to be cropped from the sink format. The size of the sink crop
0465 rectangle will also be the size of the format of the subdev's source
0466 pad.
0467
0468
0469 .. _subdev-image-processing-scaling-multi-source:
0470
0471 .. kernel-figure:: subdev-image-processing-scaling-multi-source.svg
0472 :alt: subdev-image-processing-scaling-multi-source.svg
0473 :align: center
0474
0475 **Figure 4.6. Image processing in subdevs: scaling with multiple sources**
0476
0477 In this example, the subdev is capable of first cropping, then scaling
0478 and finally cropping for two source pads individually from the resulting
0479 scaled image. The location of the scaled image in the cropped image is
0480 ignored in sink compose target. Both of the locations of the source crop
0481 rectangles refer to the sink scaling rectangle, independently cropping
0482 an area at location specified by the source crop rectangle from it.
0483
0484
0485 .. _subdev-image-processing-full:
0486
0487 .. kernel-figure:: subdev-image-processing-full.svg
0488 :alt: subdev-image-processing-full.svg
0489 :align: center
0490
0491 **Figure 4.7. Image processing in subdevs: scaling and composition with multiple sinks and sources**
0492
0493 The subdev driver supports two sink pads and two source pads. The images
0494 from both of the sink pads are individually cropped, then scaled and
0495 further composed on the composition bounds rectangle. From that, two
0496 independent streams are cropped and sent out of the subdev from the
0497 source pads.
0498
0499
0500 .. toctree::
0501 :maxdepth: 1
0502
0503 subdev-formats