Path: blob/master/Documentation/DocBook/v4l/dev-subdev.xml
10821 views
<title>Sub-device Interface</title>12<note>3<title>Experimental</title>4<para>This is an <link linkend="experimental">experimental</link>5interface and may change in the future.</para>6</note>78<para>The complex nature of V4L2 devices, where hardware is often made of9several integrated circuits that need to interact with each other in a10controlled way, leads to complex V4L2 drivers. The drivers usually reflect11the hardware model in software, and model the different hardware components12as software blocks called sub-devices.</para>1314<para>V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver15implements the media device API, they will automatically inherit from media16entities. Applications will be able to enumerate the sub-devices and discover17the hardware topology using the media entities, pads and links enumeration18API.</para>1920<para>In addition to make sub-devices discoverable, drivers can also choose21to make them directly configurable by applications. When both the sub-device22driver and the V4L2 device driver support this, sub-devices will feature a23character device node on which ioctls can be called to24<itemizedlist>25<listitem><para>query, read and write sub-devices controls</para></listitem>26<listitem><para>subscribe and unsubscribe to events and retrieve them</para></listitem>27<listitem><para>negotiate image formats on individual pads</para></listitem>28</itemizedlist>29</para>3031<para>Sub-device character device nodes, conventionally named32<filename>/dev/v4l-subdev*</filename>, use major number 81.</para>3334<section>35<title>Controls</title>36<para>Most V4L2 controls are implemented by sub-device hardware. Drivers37usually merge all controls and expose them through video device nodes.38Applications can control all sub-devices through a single interface.</para>3940<para>Complex devices sometimes implement the same control in different41pieces of hardware. This situation is common in embedded platforms, where42both sensors and image processing hardware implement identical functions,43such as contrast adjustment, white balance or faulty pixels correction. As44the V4L2 controls API doesn't support several identical controls in a single45device, all but one of the identical controls are hidden.</para>4647<para>Applications can access those hidden controls through the sub-device48node with the V4L2 control API described in <xref linkend="control" />. The49ioctls behave identically as when issued on V4L2 device nodes, with the50exception that they deal only with controls implemented in the sub-device.51</para>5253<para>Depending on the driver, those controls might also be exposed through54one (or several) V4L2 device nodes.</para>55</section>5657<section>58<title>Events</title>59<para>V4L2 sub-devices can notify applications of events as described in60<xref linkend="event" />. The API behaves identically as when used on V4L261device nodes, with the exception that it only deals with events generated by62the sub-device. Depending on the driver, those events might also be reported63on one (or several) V4L2 device nodes.</para>64</section>6566<section id="pad-level-formats">67<title>Pad-level Formats</title>6869<warning><para>Pad-level formats are only applicable to very complex device that70need to expose low-level format configuration to user space. Generic V4L271applications do <emphasis>not</emphasis> need to use the API described in72this section.</para></warning>7374<note><para>For the purpose of this section, the term75<wordasword>format</wordasword> means the combination of media bus data76format, frame width and frame height.</para></note>7778<para>Image formats are typically negotiated on video capture and output79devices using the <link linkend="crop">cropping and scaling</link> ioctls.80The driver is responsible for configuring every block in the video pipeline81according to the requested format at the pipeline input and/or82output.</para>8384<para>For complex devices, such as often found in embedded systems,85identical image sizes at the output of a pipeline can be achieved using86different hardware configurations. One such example is shown on87<xref linkend="pipeline-scaling" />, where88image scaling can be performed on both the video sensor and the host image89processing hardware.</para>9091<figure id="pipeline-scaling">92<title>Image Format Negotiation on Pipelines</title>93<mediaobject>94<imageobject>95<imagedata fileref="pipeline.pdf" format="PS" />96</imageobject>97<imageobject>98<imagedata fileref="pipeline.png" format="PNG" />99</imageobject>100<textobject>101<phrase>High quality and high speed pipeline configuration</phrase>102</textobject>103</mediaobject>104</figure>105106<para>The sensor scaler is usually of less quality than the host scaler, but107scaling on the sensor is required to achieve higher frame rates. Depending108on the use case (quality vs. speed), the pipeline must be configured109differently. Applications need to configure the formats at every point in110the pipeline explicitly.</para>111112<para>Drivers that implement the <link linkend="media-controller-intro">media113API</link> can expose pad-level image format configuration to applications.114When they do, applications can use the &VIDIOC-SUBDEV-G-FMT; and115&VIDIOC-SUBDEV-S-FMT; ioctls. to negotiate formats on a per-pad basis.</para>116117<para>Applications are responsible for configuring coherent parameters on118the whole pipeline and making sure that connected pads have compatible119formats. The pipeline is checked for formats mismatch at &VIDIOC-STREAMON;120time, and an &EPIPE; is then returned if the configuration is121invalid.</para>122123<para>Pad-level image format configuration support can be tested by calling124the &VIDIOC-SUBDEV-G-FMT; ioctl on pad 0. If the driver returns an &EINVAL;125pad-level format configuration is not supported by the sub-device.</para>126127<section>128<title>Format Negotiation</title>129130<para>Acceptable formats on pads can (and usually do) depend on a number131of external parameters, such as formats on other pads, active links, or132even controls. Finding a combination of formats on all pads in a video133pipeline, acceptable to both application and driver, can't rely on formats134enumeration only. A format negotiation mechanism is required.</para>135136<para>Central to the format negotiation mechanism are the get/set format137operations. When called with the <structfield>which</structfield> argument138set to <constant>V4L2_SUBDEV_FORMAT_TRY</constant>, the139&VIDIOC-SUBDEV-G-FMT; and &VIDIOC-SUBDEV-S-FMT; ioctls operate on a set of140formats parameters that are not connected to the hardware configuration.141Modifying those 'try' formats leaves the device state untouched (this142applies to both the software state stored in the driver and the hardware143state stored in the device itself).</para>144145<para>While not kept as part of the device state, try formats are stored146in the sub-device file handles. A &VIDIOC-SUBDEV-G-FMT; call will return147the last try format set <emphasis>on the same sub-device file148handle</emphasis>. Several applications querying the same sub-device at149the same time will thus not interact with each other.</para>150151<para>To find out whether a particular format is supported by the device,152applications use the &VIDIOC-SUBDEV-S-FMT; ioctl. Drivers verify and, if153needed, change the requested <structfield>format</structfield> based on154device requirements and return the possibly modified value. Applications155can then choose to try a different format or accept the returned value and156continue.</para>157158<para>Formats returned by the driver during a negotiation iteration are159guaranteed to be supported by the device. In particular, drivers guarantee160that a returned format will not be further changed if passed to an161&VIDIOC-SUBDEV-S-FMT; call as-is (as long as external parameters, such as162formats on other pads or links' configuration are not changed).</para>163164<para>Drivers automatically propagate formats inside sub-devices. When a165try or active format is set on a pad, corresponding formats on other pads166of the same sub-device can be modified by the driver. Drivers are free to167modify formats as required by the device. However, they should comply with168the following rules when possible:169<itemizedlist>170<listitem><para>Formats should be propagated from sink pads to source pads.171Modifying a format on a source pad should not modify the format on any172sink pad.</para></listitem>173<listitem><para>Sub-devices that scale frames using variable scaling factors174should reset the scale factors to default values when sink pads formats175are modified. If the 1:1 scaling ratio is supported, this means that176source pads formats should be reset to the sink pads formats.</para></listitem>177</itemizedlist>178</para>179180<para>Formats are not propagated across links, as that would involve181propagating them from one sub-device file handle to another. Applications182must then take care to configure both ends of every link explicitly with183compatible formats. Identical formats on the two ends of a link are184guaranteed to be compatible. Drivers are free to accept different formats185matching device requirements as being compatible.</para>186187<para><xref linkend="sample-pipeline-config" />188shows a sample configuration sequence for the pipeline described in189<xref linkend="pipeline-scaling" /> (table190columns list entity names and pad numbers).</para>191192<table pgwide="0" frame="none" id="sample-pipeline-config">193<title>Sample Pipeline Configuration</title>194<tgroup cols="3">195<colspec colname="what"/>196<colspec colname="sensor-0" />197<colspec colname="frontend-0" />198<colspec colname="frontend-1" />199<colspec colname="scaler-0" />200<colspec colname="scaler-1" />201<thead>202<row>203<entry></entry>204<entry>Sensor/0</entry>205<entry>Frontend/0</entry>206<entry>Frontend/1</entry>207<entry>Scaler/0</entry>208<entry>Scaler/1</entry>209</row>210</thead>211<tbody valign="top">212<row>213<entry>Initial state</entry>214<entry>2048x1536</entry>215<entry>-</entry>216<entry>-</entry>217<entry>-</entry>218<entry>-</entry>219</row>220<row>221<entry>Configure frontend input</entry>222<entry>2048x1536</entry>223<entry><emphasis>2048x1536</emphasis></entry>224<entry><emphasis>2046x1534</emphasis></entry>225<entry>-</entry>226<entry>-</entry>227</row>228<row>229<entry>Configure scaler input</entry>230<entry>2048x1536</entry>231<entry>2048x1536</entry>232<entry>2046x1534</entry>233<entry><emphasis>2046x1534</emphasis></entry>234<entry><emphasis>2046x1534</emphasis></entry>235</row>236<row>237<entry>Configure scaler output</entry>238<entry>2048x1536</entry>239<entry>2048x1536</entry>240<entry>2046x1534</entry>241<entry>2046x1534</entry>242<entry><emphasis>1280x960</emphasis></entry>243</row>244</tbody>245</tgroup>246</table>247248<para>249<orderedlist>250<listitem><para>Initial state. The sensor output is set to its native 3MP251resolution. Resolutions on the host frontend and scaler input and output252pads are undefined.</para></listitem>253<listitem><para>The application configures the frontend input pad resolution to2542048x1536. The driver propagates the format to the frontend output pad.255Note that the propagated output format can be different, as in this case,256than the input format, as the hardware might need to crop pixels (for257instance when converting a Bayer filter pattern to RGB or YUV).</para></listitem>258<listitem><para>The application configures the scaler input pad resolution to2592046x1534 to match the frontend output resolution. The driver propagates260the format to the scaler output pad.</para></listitem>261<listitem><para>The application configures the scaler output pad resolution to2621280x960.</para></listitem>263</orderedlist>264</para>265266<para>When satisfied with the try results, applications can set the active267formats by setting the <structfield>which</structfield> argument to268<constant>V4L2_SUBDEV_FORMAT_TRY</constant>. Active formats are changed269exactly as try formats by drivers. To avoid modifying the hardware state270during format negotiation, applications should negotiate try formats first271and then modify the active settings using the try formats returned during272the last negotiation iteration. This guarantees that the active format273will be applied as-is by the driver without being modified.274</para>275</section>276277<section>278<title>Cropping and scaling</title>279280<para>Many sub-devices support cropping frames on their input or output281pads (or possible even on both). Cropping is used to select the area of282interest in an image, typically on a video sensor or video decoder. It can283also be used as part of digital zoom implementations to select the area of284the image that will be scaled up.</para>285286<para>Crop settings are defined by a crop rectangle and represented in a287&v4l2-rect; by the coordinates of the top left corner and the rectangle288size. Both the coordinates and sizes are expressed in pixels.</para>289290<para>The crop rectangle is retrieved and set using the291&VIDIOC-SUBDEV-G-CROP; and &VIDIOC-SUBDEV-S-CROP; ioctls. Like for pad292formats, drivers store try and active crop rectangles. The format293negotiation mechanism applies to crop settings as well.</para>294295<para>On input pads, cropping is applied relatively to the current pad296format. The pad format represents the image size as received by the297sub-device from the previous block in the pipeline, and the crop rectangle298represents the sub-image that will be transmitted further inside the299sub-device for processing. The crop rectangle be entirely containted300inside the input image size.</para>301302<para>Input crop rectangle are reset to their default value when the input303image format is modified. Drivers should use the input image size as the304crop rectangle default value, but hardware requirements may prevent this.305</para>306307<para>Cropping behaviour on output pads is not defined.</para>308309</section>310</section>311312&sub-subdev-formats;313314315