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******************** 13Sub-device Interface 14******************** 15 16The complex nature of V4L2 devices, where hardware is often made of 17several integrated circuits that need to interact with each other in a 18controlled way, leads to complex V4L2 drivers. The drivers usually 19reflect the hardware model in software, and model the different hardware 20components as software blocks called sub-devices. 21 22V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver 23implements the media device API, they will automatically inherit from 24media entities. Applications will be able to enumerate the sub-devices 25and discover the hardware topology using the media entities, pads and 26links enumeration API. 27 28In addition to make sub-devices discoverable, drivers can also choose to 29make them directly configurable by applications. When both the 30sub-device driver and the V4L2 device driver support this, sub-devices 31will 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 39Sub-device character device nodes, conventionally named 40``/dev/v4l-subdev*``, use major number 81. 41 42Drivers may opt to limit the sub-device character devices to only expose 43operations that do not modify the device state. In such a case the sub-devices 44are referred to as ``read-only`` in the rest of this documentation, and the 45related restrictions are documented in individual ioctls. 46 47 48Controls 49======== 50 51Most V4L2 controls are implemented by sub-device hardware. Drivers 52usually merge all controls and expose them through video device nodes. 53Applications can control all sub-devices through a single interface. 54 55Complex devices sometimes implement the same control in different pieces 56of hardware. This situation is common in embedded platforms, where both 57sensors and image processing hardware implement identical functions, 58such as contrast adjustment, white balance or faulty pixels correction. 59As the V4L2 controls API doesn't support several identical controls in a 60single device, all but one of the identical controls are hidden. 61 62Applications can access those hidden controls through the sub-device 63node with the V4L2 control API described in :ref:`control`. The ioctls 64behave identically as when issued on V4L2 device nodes, with the 65exception that they deal only with controls implemented in the 66sub-device. 67 68Depending on the driver, those controls might also be exposed through 69one (or several) V4L2 device nodes. 70 71 72Events 73====== 74 75V4L2 sub-devices can notify applications of events as described in 76:ref:`event`. The API behaves identically as when used on V4L2 device 77nodes, with the exception that it only deals with events generated by 78the sub-device. Depending on the driver, those events might also be 79reported on one (or several) V4L2 device nodes. 80 81 82.. _pad-level-formats: 83 84Pad-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 99Image formats are typically negotiated on video capture and output 100devices using the format and 101:ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is 102responsible for configuring every block in the video pipeline according 103to the requested format at the pipeline input and/or output. 104 105For complex devices, such as often found in embedded systems, identical 106image sizes at the output of a pipeline can be achieved using different 107hardware configurations. One such example is shown on 108:ref:`pipeline-scaling`, where image scaling can be performed on both 109the 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 124The sensor scaler is usually of less quality than the host scaler, but 125scaling on the sensor is required to achieve higher frame rates. 126Depending on the use case (quality vs. speed), the pipeline must be 127configured differently. Applications need to configure the formats at 128every point in the pipeline explicitly. 129 130Drivers that implement the :ref:`media API <media-controller-intro>` 131can expose pad-level image format configuration to applications. When 132they 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 135negotiate formats on a per-pad basis. 136 137Applications are responsible for configuring coherent parameters on the 138whole pipeline and making sure that connected pads have compatible 139formats. The pipeline is checked for formats mismatch at 140:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error 141code is then returned if the configuration is invalid. 142 143Pad-level image format configuration support can be tested by calling 144the :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad 1450. If the driver returns an ``EINVAL`` error code pad-level format 146configuration is not supported by the sub-device. 147 148 149Format Negotiation 150------------------ 151 152Acceptable formats on pads can (and usually do) depend on a number of 153external parameters, such as formats on other pads, active links, or 154even controls. Finding a combination of formats on all pads in a video 155pipeline, acceptable to both application and driver, can't rely on 156formats enumeration only. A format negotiation mechanism is required. 157 158Central to the format negotiation mechanism are the get/set format 159operations. 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 163a set of formats parameters that are not connected to the hardware 164configuration. Modifying those 'try' formats leaves the device state 165untouched (this applies to both the software state stored in the driver 166and the hardware state stored in the device itself). 167 168While not kept as part of the device state, try formats are stored in 169the sub-device file handles. A 170:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return 171the last try format set *on the same sub-device file handle*. Several 172applications querying the same sub-device at the same time will thus not 173interact with each other. 174 175To find out whether a particular format is supported by the device, 176applications use the 177:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers 178verify and, if needed, change the requested ``format`` based on device 179requirements and return the possibly modified value. Applications can 180then choose to try a different format or accept the returned value and 181continue. 182 183Formats returned by the driver during a negotiation iteration are 184guaranteed to be supported by the device. In particular, drivers 185guarantee that a returned format will not be further changed if passed 186to 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' 188configuration are not changed). 189 190Drivers automatically propagate formats inside sub-devices. When a try 191or active format is set on a pad, corresponding formats on other pads of 192the same sub-device can be modified by the driver. Drivers are free to 193modify formats as required by the device. However, they should comply 194with 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 205Formats are not propagated across links, as that would involve 206propagating them from one sub-device file handle to another. 207Applications must then take care to configure both ends of every link 208explicitly with compatible formats. Identical formats on the two ends of 209a link are guaranteed to be compatible. Drivers are free to accept 210different formats matching device requirements as being compatible. 211 212:ref:`sample-pipeline-config` shows a sample configuration sequence 213for the pipeline described in :ref:`pipeline-scaling` (table columns 214list 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 3101. 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 3152. 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 3193. 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 3264. 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 330When satisfied with the try results, applications can set the active 331formats by setting the ``which`` argument to 332``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try 333formats by drivers. To avoid modifying the hardware state during format 334negotiation, applications should negotiate try formats first and then 335modify the active settings using the try formats returned during the 336last negotiation iteration. This guarantees that the active format will 337be applied as-is by the driver without being modified. 338 339 340.. _v4l2-subdev-selections: 341 342Selections: cropping, scaling and composition 343--------------------------------------------- 344 345Many 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 347interest in an image, typically on an image sensor or a video decoder. 348It can also be used as part of digital zoom implementations to select 349the area of the image that will be scaled up. 350 351Crop settings are defined by a crop rectangle and represented in a 352struct :c:type:`v4l2_rect` by the coordinates of the top 353left corner and the rectangle size. Both the coordinates and sizes are 354expressed in pixels. 355 356As for pad formats, drivers store try and active rectangles for the 357selection targets :ref:`v4l2-selections-common`. 358 359On sink pads, cropping is applied relative to the current pad format. 360The pad format represents the image size as received by the sub-device 361from the previous block in the pipeline, and the crop rectangle 362represents the sub-image that will be transmitted further inside the 363sub-device for processing. 364 365The scaling operation changes the size of the image by scaling it to new 366dimensions. The scaling ratio isn't specified explicitly, but is implied 367from the original and scaled image sizes. Both sizes are represented by 368struct :c:type:`v4l2_rect`. 369 370Scaling support is optional. When supported by a subdev, the crop 371rectangle on the subdev's sink pad is scaled to the size configured 372using the 373:ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL 374using ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the 375subdev supports scaling but not composing, the top and left values are 376not used and must always be set to zero. 377 378On source pads, cropping is similar to sink pads, with the exception 379that the source size from which the cropping is performed, is the 380COMPOSE rectangle on the sink pad. In both sink and source pads, the 381crop rectangle must be entirely contained inside the source image size 382for the crop operation. 383 384The drivers should always use the closest possible rectangle the user 385requests on all selection targets, unless specifically told otherwise. 386``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round 387the image size either up or down. :ref:`v4l2-selection-flags` 388 389 390Types of selection targets 391-------------------------- 392 393 394Actual targets 395^^^^^^^^^^^^^^ 396 397Actual targets (without a postfix) reflect the actual hardware 398configuration at any point of time. There is a BOUNDS target 399corresponding to every actual target. 400 401 402BOUNDS targets 403^^^^^^^^^^^^^^ 404 405BOUNDS targets is the smallest rectangle that contains all valid actual 406rectangles. It may not be possible to set the actual rectangle as large 407as the BOUNDS rectangle, however. This may be because e.g. a sensor's 408pixel array is not rectangular but cross-shaped or round. The maximum 409size may also be smaller than the BOUNDS rectangle. 410 411 412Order of configuration and format propagation 413--------------------------------------------- 414 415Inside subdevs, the order of image processing steps will always be from 416the sink pad towards the source pad. This is also reflected in the order 417in which the configuration must be performed by the user: the changes 418made will be propagated to any subsequent stages. If this behaviour is 419not desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This 420flag causes no propagation of the changes are allowed in any 421circumstances. This may also cause the accessed rectangle to be adjusted 422by the driver, depending on the properties of the underlying hardware. 423 424The coordinates to a step always refer to the actual size of the 425previous step. The exception to this rule is the sink compose 426rectangle, which refers to the sink compose bounds rectangle --- if it 427is supported by the hardware. 428 4291. 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 4332. Sink pad actual crop selection. The sink pad crop defines the crop 434 performed to the sink pad format. 435 4363. 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 4424. Source pad actual crop selection. Crop on the source pad defines crop 443 performed to the image in the sink compose bounds rectangle. 444 4455. 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 450Accessing any of the above rectangles not supported by the subdev will 451return ``EINVAL``. Any rectangle referring to a previous unsupported 452rectangle coordinates will instead refer to the previous supported 453rectangle. For example, if sink crop is not supported, the compose 454selection 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 465In the above example, the subdev supports cropping on its sink pad. To 466configure it, the user sets the media bus format on the subdev's sink 467pad. Now the actual crop rectangle can be set on the sink pad --- the 468location and size of this rectangle reflect the location and size of a 469rectangle to be cropped from the sink format. The size of the sink crop 470rectangle will also be the size of the format of the subdev's source 471pad. 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 482In this example, the subdev is capable of first cropping, then scaling 483and finally cropping for two source pads individually from the resulting 484scaled image. The location of the scaled image in the cropped image is 485ignored in sink compose target. Both of the locations of the source crop 486rectangles refer to the sink scaling rectangle, independently cropping 487an 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 498The subdev driver supports two sink pads and two source pads. The images 499from both of the sink pads are individually cropped, then scaled and 500further composed on the composition bounds rectangle. From that, two 501independent streams are cropped and sent out of the subdev from the 502source pads. 503 504 505.. toctree:: 506 :maxdepth: 1 507 508 subdev-formats 509