# Media controllers

---

# Introduction

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> Media devices increasingly handle multiple related functions. Many USB cameras include microphones, video capture hardware can also output video, or SoC camera interfaces also perform memory-to-memory operations similar to video codecs.

<a id="rst-introduction"></a>
<a id="rst-media-controller-intro"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

Media devices increasingly handle multiple related functions. Many USB cameras include microphones, video capture hardware can also output video, or SoC camera interfaces also perform memory-to-memory operations similar to video codecs.

Independent functions, even when implemented in the same hardware, can be modelled as separate devices. A USB camera with a microphone will be presented to userspace applications as V4L2 and ALSA capture devices. The devices' relationships (when using a webcam, end-users shouldn't have to manually select the associated USB microphone), while not made available directly to applications by the drivers, can usually be retrieved from sysfs.

With more and more advanced SoC devices being introduced, the current approach will not scale. Device topologies are getting increasingly complex and can't always be represented by a tree structure. Hardware blocks are shared between different functions, creating dependencies between seemingly unrelated devices.

Kernel abstraction APIs such as V4L2 and ALSA provide means for applications to access hardware parameters. As newer hardware expose an increasingly high number of those parameters, drivers need to guess what applications really require based on limited information, thereby implementing policies that belong to userspace.

The media controller API aims at solving those problems.

---

# Media device model

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> Discovering a device internal topology, and configuring it at runtime, is one of the goals of the media controller API. To achieve this, hardware devices and Linux Kernel interfaces are modelled as graph objects on an oriented graph. The…

<a id="rst-media-device-model"></a>
<a id="rst-media-controller-model"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

Discovering a device internal topology, and configuring it at runtime, is one of the goals of the media controller API. To achieve this, hardware devices and Linux Kernel interfaces are modelled as graph objects on an oriented graph. The object types that constitute the graph are:

- An **entity** is a basic media hardware or software building block. It can correspond to a large variety of logical blocks such as physical hardware devices (CMOS sensor for instance), logical hardware devices (a building block in a System-on-Chip image processing pipeline), DMA channels or physical connectors.

- An **interface** is a graph representation of a Linux Kernel userspace API interface, like a device node or a sysfs file that controls one or more entities in the graph.

- A **pad** is a data connection endpoint through which an entity can interact with other entities. Data (not restricted to video) produced by an entity flows from the entity's output to one or more entity inputs. Pads should not be confused with physical pins at chip boundaries.

- A **data link** is a point-to-point oriented connection between two pads, either on the same entity or on different entities. Data flows from a source pad to a sink pad.

- An **interface link** is a point-to-point bidirectional control connection between a Linux Kernel interface and an entity.

- An **ancillary link** is a point-to-point connection denoting that two entities form a single logical unit. For example this could represent the fact that a particular camera sensor and lens controller form a single physical module, meaning this lens controller drives the lens for this camera sensor.

---

# Part IV - Media Controller API

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> Authors:

<a id="rst-part-iv-media-controller-api"></a>
<a id="rst-media-controller"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- This data file has been placed in the public domain. -->

<!-- Derived from the Unicode character mappings available from
<http://www.w3.org/2003/entities/xml/>.
Processed by unicode2rstsubs.py, part of Docutils:
<https://docutils.sourceforge.io>. -->

**Table of Contents**

1. [Introduction](</kernel/userspace/userspace-api/media-mediactl/media-controller-intro.md>)
2. [Media device model](</kernel/userspace/userspace-api/media-mediactl/media-controller-model.md>)
3. [Types and flags used to represent the media graph elements](</kernel/userspace/userspace-api/media-mediactl/media-types.md>)
4. [Request API](</kernel/userspace/userspace-api/media-mediactl/request-api.md>)
5. [Function Reference](</kernel/userspace/userspace-api/media-mediactl/media-funcs.md>)
6. [media-header](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/mediactl/media-header.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>)

<a id="rst-revision-and-copyright"></a>

## Revision and Copyright

Authors:

- Pinchart, Laurent &lt;[laurent.pinchart@ideasonboard.com](<mailto:laurent.pinchart@ideasonboard.com>)&gt;

> - Initial version.

- Carvalho Chehab, Mauro &lt;[mchehab@kernel.org](<mailto:mchehab@kernel.org>)&gt;

> - MEDIA_IOC_G_TOPOLOGY documentation and documentation improvements.

**Copyright** © 2010 : Laurent Pinchart

**Copyright** © 2015-2016 : Mauro Carvalho Chehab

<a id="rst-revision-history"></a>

## Revision History

**revision:** 1.1.0 / 2015-12-12 (*mcc*)

**revision:** 1.0.0 / 2010-11-10 (*lp*)

Initial revision

---

# media close()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> media-close - Close a media device

<a id="rst-media-close"></a>
<a id="rst-media-func-close"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

media-close - Close a media device

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <unistd.h>
```





<a id="c-close"></a>

```c
int close( int fd )
```

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

<a id="rst-description"></a>

## Description

Closes the media device. Resources associated with the file descriptor are freed. The device configuration remain unchanged.

<a id="rst-return-value"></a>

## Return Value

`close()` returns 0 on success. On error, -1 is returned, and `errno` is set appropriately. Possible error codes are:

- **EBADF**

  `fd` is not a valid open file descriptor.

---

# media ioctl()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> media-ioctl - Control a media device

<a id="rst-media-ioctl"></a>
<a id="rst-media-func-ioctl"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

media-ioctl - Control a media device

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <sys/ioctl.h>
```

`int ioctl(int fd, int request, void *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`request`**

  Media ioctl request code as defined in the media.h header file, for example MEDIA_IOC_SETUP_LINK.

- **`argp`**

  Pointer to a request-specific structure.

<a id="rst-description"></a>

## Description

The [ioctl()](<#rst-media-func-ioctl>) function manipulates media device parameters. The argument `fd` must be an open file descriptor.

The ioctl `request` code specifies the media function to be called. It has encoded in it whether the argument is an input, output or read/write parameter, and the size of the argument `argp` in bytes.

Macros and structures definitions specifying media ioctl requests and their parameters are located in the media.h header file. All media ioctl requests, their respective function and parameters are specified in [Function Reference](</kernel/userspace/userspace-api/media-mediactl/media-funcs.md#rst-media-user-func>).

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

Request-specific error codes are listed in the individual requests descriptions.

When an ioctl that takes an output or read/write parameter fails, the parameter remains unmodified.

---

# media open()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> media-open - Open a media device

<a id="rst-media-open"></a>
<a id="rst-media-func-open"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

media-open - Open a media device

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <fcntl.h>
```





<a id="c-open"></a>

```c
int open( const char *device_name, int flags )
```

<a id="rst-arguments"></a>

## Arguments

- **`device_name`**

  Device to be opened.

- **`flags`**

  Open flags. Access mode must be either `O_RDONLY` or `O_RDWR`. Other flags have no effect.

<a id="rst-description"></a>

## Description

To open a media device applications call `open()` with the desired device name. The function has no side effects; the device configuration remain unchanged.

When the device is opened in read-only mode, attempts to modify its configuration will result in an error, and `errno` will be set to EBADF.

<a id="rst-return-value"></a>

## Return Value

`open()` returns the new file descriptor on success. On error, -1 is returned, and `errno` is set appropriately. Possible error codes are:

- **EACCES**

  The requested access to the file is not allowed.

- **EMFILE**

  The process already has the maximum number of files open.

- **ENFILE**

  The system limit on the total number of open files has been reached.

- **ENOMEM**

  Insufficient kernel memory was available.

- **ENXIO**

  No device corresponding to this device special file exists.

---

# Function Reference

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> Reference for Function Reference.

<a id="rst-function-reference"></a>
<a id="rst-media-user-func"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

- [media open()](</kernel/userspace/userspace-api/media-mediactl/media-func-open.md>)
- [media close()](</kernel/userspace/userspace-api/media-mediactl/media-func-close.md>)
- [media ioctl()](</kernel/userspace/userspace-api/media-mediactl/media-func-ioctl.md>)
- [ioctl MEDIA_IOC_DEVICE_INFO](</kernel/userspace/userspace-api/media-mediactl/media-ioc-device-info.md>)
- [ioctl MEDIA_IOC_G_TOPOLOGY](</kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md>)
- [ioctl MEDIA_IOC_ENUM_ENTITIES](</kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md>)
- [ioctl MEDIA_IOC_ENUM_LINKS](</kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-links.md>)
- [ioctl MEDIA_IOC_SETUP_LINK](</kernel/userspace/userspace-api/media-mediactl/media-ioc-setup-link.md>)
- [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md>)
- [request close()](</kernel/userspace/userspace-api/media-mediactl/request-func-close.md>)
- [request ioctl()](</kernel/userspace/userspace-api/media-mediactl/request-func-ioctl.md>)
- [request poll()](</kernel/userspace/userspace-api/media-mediactl/request-func-poll.md>)
- [ioctl MEDIA_REQUEST_IOC_QUEUE](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-queue.md>)
- [ioctl MEDIA_REQUEST_IOC_REINIT](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-reinit.md>)

---

# Media controller uAPI symbols

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> Reference for Media controller uAPI symbols.

<a id="rst-media-controller-uapi-symbols"></a>
<a id="rst-media-header"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->


## IOCTL Commands

- [`MEDIA_IOC_DEVICE_INFO`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-device-info.md#rst-media-ioc-device-info): `include/uapi/linux/media.h`, line 371
- [`MEDIA_IOC_ENUM_ENTITIES`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md#rst-media-ioc-enum-entities): `include/uapi/linux/media.h`, line 372
- [`MEDIA_IOC_ENUM_LINKS`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-links.md#rst-media-ioc-enum-links): `include/uapi/linux/media.h`, line 373
- [`MEDIA_IOC_G_TOPOLOGY`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#rst-media-ioc-g-topology): `include/uapi/linux/media.h`, line 375
- [`MEDIA_IOC_REQUEST_ALLOC`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc): `include/uapi/linux/media.h`, line 376
- [`MEDIA_IOC_SETUP_LINK`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-setup-link.md#rst-media-ioc-setup-link): `include/uapi/linux/media.h`, line 374
- [`MEDIA_REQUEST_IOC_QUEUE`](/kernel/userspace/userspace-api/media-mediactl/media-request-ioc-queue.md#rst-media-request-ioc-queue): `include/uapi/linux/media.h`, line 382
- [`MEDIA_REQUEST_IOC_REINIT`](/kernel/userspace/userspace-api/media-mediactl/media-request-ioc-reinit.md#rst-media-request-ioc-reinit): `include/uapi/linux/media.h`, line 383

## Macros and Definitions

- [`MEDIA_ENT_FL_CONNECTOR`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-fl-connector): `include/uapi/linux/media.h`, line 144
- [`MEDIA_ENT_FL_DEFAULT`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-fl-default): `include/uapi/linux/media.h`, line 143
- [`MEDIA_ENT_F_ATV_DECODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-atv-decoder): `include/uapi/linux/media.h`, line 138
- [`MEDIA_ENT_F_AUDIO_CAPTURE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-audio-capture): `include/uapi/linux/media.h`, line 112
- [`MEDIA_ENT_F_AUDIO_MIXER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-audio-mixer): `include/uapi/linux/media.h`, line 114
- [`MEDIA_ENT_F_AUDIO_PLAYBACK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-audio-playback): `include/uapi/linux/media.h`, line 113
- [`MEDIA_ENT_F_CAM_SENSOR`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-cam-sensor): `include/uapi/linux/media.h`, line 84
- [`MEDIA_ENT_F_CONN_COMPOSITE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-conn-composite): `include/uapi/linux/media.h`, line 281
- [`MEDIA_ENT_F_CONN_RF`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-conn-rf): `include/uapi/linux/media.h`, line 279
- [`MEDIA_ENT_F_CONN_SVIDEO`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-conn-svideo): `include/uapi/linux/media.h`, line 280
- [`MEDIA_ENT_F_DTV_CA`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-dtv-ca): `include/uapi/linux/media.h`, line 70
- [`MEDIA_ENT_F_DTV_DEMOD`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-dtv-demod): `include/uapi/linux/media.h`, line 68
- [`MEDIA_ENT_F_DTV_NET_DECAP`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-dtv-net-decap): `include/uapi/linux/media.h`, line 71
- [`MEDIA_ENT_F_DV_DECODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-dv-decoder): `include/uapi/linux/media.h`, line 139
- [`MEDIA_ENT_F_DV_ENCODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-dv-encoder): `include/uapi/linux/media.h`, line 140
- [`MEDIA_ENT_F_FLASH`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-flash): `include/uapi/linux/media.h`, line 85
- [`MEDIA_ENT_F_IF_AUD_DECODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-if-aud-decoder): `include/uapi/linux/media.h`, line 107
- [`MEDIA_ENT_F_IF_VID_DECODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-if-vid-decoder): `include/uapi/linux/media.h`, line 106
- [`MEDIA_ENT_F_IO_DTV`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-io-dtv): `include/uapi/linux/media.h`, line 77
- [`MEDIA_ENT_F_IO_SWRADIO`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-io-swradio): `include/uapi/linux/media.h`, line 79
- [`MEDIA_ENT_F_IO_V4L`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-io-v4l): `include/uapi/linux/media.h`, line 76
- [`MEDIA_ENT_F_IO_VBI`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-io-vbi): `include/uapi/linux/media.h`, line 78
- [`MEDIA_ENT_F_LENS`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-lens): `include/uapi/linux/media.h`, line 86
- [`MEDIA_ENT_F_PROC_VIDEO_COMPOSER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-composer): `include/uapi/linux/media.h`, line 119
- [`MEDIA_ENT_F_PROC_VIDEO_DECODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-decoder): `include/uapi/linux/media.h`, line 126
- [`MEDIA_ENT_F_PROC_VIDEO_ENCODER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-encoder): `include/uapi/linux/media.h`, line 125
- [`MEDIA_ENT_F_PROC_VIDEO_ISP`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-isp): `include/uapi/linux/media.h`, line 127
- [`MEDIA_ENT_F_PROC_VIDEO_LUT`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-lut): `include/uapi/linux/media.h`, line 122
- [`MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-pixel-enc-conv): `include/uapi/linux/media.h`, line 121
- [`MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-pixel-formatter): `include/uapi/linux/media.h`, line 120
- [`MEDIA_ENT_F_PROC_VIDEO_SCALER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-scaler): `include/uapi/linux/media.h`, line 123
- [`MEDIA_ENT_F_PROC_VIDEO_STATISTICS`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-proc-video-statistics): `include/uapi/linux/media.h`, line 124
- [`MEDIA_ENT_F_TS_DEMUX`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-ts-demux): `include/uapi/linux/media.h`, line 69
- [`MEDIA_ENT_F_TUNER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-tuner): `include/uapi/linux/media.h`, line 98
- [`MEDIA_ENT_F_UNKNOWN`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-unknown): `include/uapi/linux/media.h`, line 56
- [`MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-v4l2-subdev-unknown): `include/uapi/linux/media.h`, line 63
- [`MEDIA_ENT_F_VID_IF_BRIDGE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-vid-if-bridge): `include/uapi/linux/media.h`, line 133
- [`MEDIA_ENT_F_VID_MUX`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-ent-f-vid-mux): `include/uapi/linux/media.h`, line 132
- [`MEDIA_ENT_ID_FLAG_NEXT`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md#rst-media-ent-id-flag-next): `include/uapi/linux/media.h`, line 147
- [`MEDIA_INTF_T_ALSA_COMPRESS`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-compress): `include/uapi/linux/media.h`, line 428
- [`MEDIA_INTF_T_ALSA_CONTROL`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-control): `include/uapi/linux/media.h`, line 267
- [`MEDIA_INTF_T_ALSA_HWDEP`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-hwdep): `include/uapi/linux/media.h`, line 430
- [`MEDIA_INTF_T_ALSA_PCM_CAPTURE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-pcm-capture): `include/uapi/linux/media.h`, line 265
- [`MEDIA_INTF_T_ALSA_PCM_PLAYBACK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-pcm-playback): `include/uapi/linux/media.h`, line 266
- [`MEDIA_INTF_T_ALSA_RAWMIDI`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-rawmidi): `include/uapi/linux/media.h`, line 429
- [`MEDIA_INTF_T_ALSA_SEQUENCER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-sequencer): `include/uapi/linux/media.h`, line 431
- [`MEDIA_INTF_T_ALSA_TIMER`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-alsa-timer): `include/uapi/linux/media.h`, line 432
- [`MEDIA_INTF_T_DVB_CA`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-dvb-ca): `include/uapi/linux/media.h`, line 254
- [`MEDIA_INTF_T_DVB_DEMUX`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-dvb-demux): `include/uapi/linux/media.h`, line 252
- [`MEDIA_INTF_T_DVB_DVR`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-dvb-dvr): `include/uapi/linux/media.h`, line 253
- [`MEDIA_INTF_T_DVB_FE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-dvb-fe): `include/uapi/linux/media.h`, line 251
- [`MEDIA_INTF_T_DVB_NET`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-dvb-net): `include/uapi/linux/media.h`, line 255
- [`MEDIA_INTF_T_V4L_RADIO`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-radio): `include/uapi/linux/media.h`, line 259
- [`MEDIA_INTF_T_V4L_SUBDEV`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-subdev): `include/uapi/linux/media.h`, line 260
- [`MEDIA_INTF_T_V4L_SWRADIO`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-swradio): `include/uapi/linux/media.h`, line 261
- [`MEDIA_INTF_T_V4L_TOUCH`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-touch): `include/uapi/linux/media.h`, line 262
- [`MEDIA_INTF_T_V4L_VBI`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-vbi): `include/uapi/linux/media.h`, line 258
- [`MEDIA_INTF_T_V4L_VIDEO`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-t-v4l-video): `include/uapi/linux/media.h`, line 257
- [`MEDIA_LNK_FL_ANCILLARY_LINK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-ancillary-link): `include/uapi/linux/media.h`, line 226
- [`MEDIA_LNK_FL_DATA_LINK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-data-link): `include/uapi/linux/media.h`, line 224
- [`MEDIA_LNK_FL_DYNAMIC`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-dynamic): `include/uapi/linux/media.h`, line 221
- [`MEDIA_LNK_FL_ENABLED`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-enabled): `include/uapi/linux/media.h`, line 219
- [`MEDIA_LNK_FL_IMMUTABLE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-immutable): `include/uapi/linux/media.h`, line 220
- [`MEDIA_LNK_FL_INTERFACE_LINK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-interface-link): `include/uapi/linux/media.h`, line 225
- [`MEDIA_LNK_FL_LINK_TYPE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-lnk-fl-link-type): `include/uapi/linux/media.h`, line 223
- [`MEDIA_PAD_FL_MUST_CONNECT`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-pad-fl-must-connect): `include/uapi/linux/media.h`, line 210
- [`MEDIA_PAD_FL_SINK`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-pad-fl-sink): `include/uapi/linux/media.h`, line 208
- [`MEDIA_PAD_FL_SOURCE`](/kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-pad-fl-source): `include/uapi/linux/media.h`, line 209

## Structures

- [`media_device_info`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-device-info.md#c-media_device_info): `include/uapi/linux/media.h`, line 26
- [`media_entity_desc`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md#c-media_entity_desc): `include/uapi/linux/media.h`, line 149
- [`media_link_desc`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-links.md#c-media_link_desc): `include/uapi/linux/media.h`, line 228
- [`media_links_enum`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-links.md#c-media_links_enum): `include/uapi/linux/media.h`, line 235
- [`media_pad_desc`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-links.md#c-media_pad_desc): `include/uapi/linux/media.h`, line 212
- [`media_v2_entity`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_entity): `include/uapi/linux/media.h`, line 298
- [`media_v2_interface`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_interface): `include/uapi/linux/media.h`, line 312
- [`media_v2_intf_devnode`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_intf_devnode): `include/uapi/linux/media.h`, line 307
- [`media_v2_link`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_link): `include/uapi/linux/media.h`, line 341
- [`media_v2_pad`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_pad): `include/uapi/linux/media.h`, line 333
- [`media_v2_topology`](/kernel/userspace/userspace-api/media-mediactl/media-ioc-g-topology.md#c-media_v2_topology): `include/uapi/linux/media.h`, line 349

---

# ioctl MEDIA_IOC_DEVICE_INFO

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_DEVICE_INFO - Query device information

<a id="rst-ioctl-media-ioc-device-info"></a>
<a id="rst-media-ioc-device-info"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_DEVICE_INFO - Query device information

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_DEVICE_INFO"></a>

```c
MEDIA_IOC_DEVICE_INFO
```

`int ioctl(int fd, MEDIA_IOC_DEVICE_INFO, struct media_device_info *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to struct `media_device_info`.

<a id="rst-description"></a>

## Description

All media devices must support the `MEDIA_IOC_DEVICE_INFO` ioctl. To query device information, applications call the ioctl with a pointer to a struct `media_device_info`. The driver fills the structure and returns the information to the application. The ioctl never fails.





<a id="c-media_device_info"></a>

```c
media_device_info
```

<!-- tabularcolumns: |p{4.4cm}|p{4.4cm}|p{8.5cm}| -->


<table>
<caption>struct media_device_info</caption>
<tbody>
<tr>
<td>


char

</td>
<td>


`driver`\[16\]

</td>
<td>


Name of the driver implementing the media API as a NUL-terminated ASCII string. The driver version is stored in the `driver_version` field.

Driver specific applications can use this information to verify the driver identity. It is also useful to work around known bugs, or to identify drivers in error reports.

</td>
</tr>
<tr>
<td>


char

</td>
<td>


`model`\[32\]

</td>
<td>


Device model name as a NUL-terminated UTF-8 string. The device version is stored in the `device_version` field and is not be appended to the model name.

</td>
</tr>
<tr>
<td>


char

</td>
<td>


`serial`\[40\]

</td>
<td>


Serial number as a NUL-terminated ASCII string.

</td>
</tr>
<tr>
<td>


char

</td>
<td>


`bus_info`\[32\]

</td>
<td>


Location of the device in the system as a NUL-terminated ASCII string. This includes the bus type name (PCI, USB, ...) and a bus-specific identifier.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`media_version`

</td>
<td>


Media API version, formatted with the `KERNEL_VERSION()` macro.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`hw_revision`

</td>
<td>


Hardware device revision in a driver-specific format.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`driver_version`

</td>
<td>


Media device driver version, formatted with the `KERNEL_VERSION()` macro. Together with the `driver` field this identifies a particular driver.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved`\[31\]

</td>
<td>


Reserved for future extensions. Drivers and applications must set this array to zero.

</td>
</tr>
</tbody>
</table>

The `serial` and `bus_info` fields can be used to distinguish between multiple instances of otherwise identical hardware. The serial number takes precedence when provided and can be assumed to be unique. If the serial number is an empty string, the `bus_info` field can be used instead. The `bus_info` field is guaranteed to be unique, but can vary across reboots or device unplug/replug.

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

---

# ioctl MEDIA_IOC_ENUM_ENTITIES

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_ENUM_ENTITIES - Enumerate entities and their properties

<a id="rst-ioctl-media-ioc-enum-entities"></a>
<a id="rst-media-ioc-enum-entities"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_ENUM_ENTITIES - Enumerate entities and their properties

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_ENUM_ENTITIES"></a>

```c
MEDIA_IOC_ENUM_ENTITIES
```

`int ioctl(int fd, MEDIA_IOC_ENUM_ENTITIES, struct media_entity_desc *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to struct `media_entity_desc`.

<a id="rst-description"></a>

## Description

To query the attributes of an entity, applications set the id field of a struct `media_entity_desc` structure and call the MEDIA_IOC_ENUM_ENTITIES ioctl with a pointer to this structure. The driver fills the rest of the structure or returns an EINVAL error code when the id is invalid.

<a id="rst-media-ent-id-flag-next"></a>

Entities can be enumerated by or'ing the id with the `MEDIA_ENT_ID_FLAG_NEXT` flag. The driver will return information about the entity with the smallest id strictly larger than the requested one ('next entity'), or the `EINVAL` error code if there is none.

Entity IDs can be non-contiguous. Applications must *not* try to enumerate entities by calling MEDIA_IOC_ENUM_ENTITIES with increasing id's until they get an error.





<a id="c-media_entity_desc"></a>

```c
media_entity_desc
```

<!-- tabularcolumns: |p{1.5cm}|p{1.7cm}|p{1.6cm}|p{1.5cm}|p{10.6cm}| -->


<table>
<caption>struct media_entity_desc</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`id`

</td>
<td>




</td>
<td>


Entity ID, set by the application. When the ID is or'ed with `MEDIA_ENT_ID_FLAG_NEXT`, the driver clears the flag and returns the first entity with a larger ID. Do not expect that the ID will always be the same for each instance of the device. In other words, do not hardcode entity IDs in an application.

</td>
</tr>
<tr>
<td>


char

</td>
<td>


`name`\[32\]

</td>
<td>




</td>
<td>


Entity name as an UTF-8 NULL-terminated string. This name must be unique within the media topology.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`type`

</td>
<td>




</td>
<td>


Entity type, see [media-entity-functions](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-entity-functions>) for details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`revision`

</td>
<td>




</td>
<td>


Entity revision. Always zero (obsolete)

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>




</td>
<td>


Entity flags, see [media-entity-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-entity-flag>) for details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`group_id`

</td>
<td>




</td>
<td>


Entity group ID. Always zero (obsolete)

</td>
</tr>
<tr>
<td>


\__u16

</td>
<td>


`pads`

</td>
<td>




</td>
<td>


Number of pads

</td>
</tr>
<tr>
<td>


\__u16

</td>
<td>


`links`

</td>
<td>




</td>
<td>


Total number of outbound links. Inbound links are not counted in this field.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved[4]`

</td>
<td>




</td>
<td>


Reserved for future extensions. Drivers and applications must set the array to zero.

</td>
</tr>
<tr>
<td>


union \{

</td>
<td colspan="3">


(anonymous)

</td>
</tr>
<tr>
<td>


struct

</td>
<td>


`dev`

</td>
<td>




</td>
<td>


Valid for (sub-)devices that create a single device node.

</td>
</tr>
<tr>
<td>




</td>
<td>


\__u32

</td>
<td>


`major`

</td>
<td>


Device node major number.

</td>
</tr>
<tr>
<td>




</td>
<td>


\__u32

</td>
<td>


`minor`

</td>
<td>


Device node minor number.

</td>
</tr>
<tr>
<td>


\__u8

</td>
<td>


`raw`\[184\]

</td>
<td>




</td>
<td>




</td>
</tr>
<tr>
<td>


\}

</td>
<td colspan="3">




</td>
</tr>
</tbody>
</table>

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **EINVAL**

  The struct `media_entity_desc` `id` references a non-existing entity.

---

# ioctl MEDIA_IOC_ENUM_LINKS

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_ENUM_LINKS - Enumerate all pads and links for a given entity

<a id="rst-ioctl-media-ioc-enum-links"></a>
<a id="rst-media-ioc-enum-links"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_ENUM_LINKS - Enumerate all pads and links for a given entity

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_ENUM_LINKS"></a>

```c
MEDIA_IOC_ENUM_LINKS
```

`int ioctl(int fd, MEDIA_IOC_ENUM_LINKS, struct media_links_enum *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to struct `media_links_enum`.

<a id="rst-description"></a>

## Description

To enumerate pads and/or links for a given entity, applications set the entity field of a struct `media_links_enum` structure and initialize the struct `media_pad_desc` and struct `media_link_desc` structure arrays pointed by the `pads` and `links` fields. They then call the MEDIA_IOC_ENUM_LINKS ioctl with a pointer to this structure.

If the `pads` field is not NULL, the driver fills the `pads` array with information about the entity's pads. The array must have enough room to store all the entity's pads. The number of pads can be retrieved with [ioctl MEDIA_IOC_ENUM_ENTITIES](</kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md#rst-media-ioc-enum-entities>).

If the `links` field is not NULL, the driver fills the `links` array with information about the entity's outbound links. The array must have enough room to store all the entity's outbound links. The number of outbound links can be retrieved with [ioctl MEDIA_IOC_ENUM_ENTITIES](</kernel/userspace/userspace-api/media-mediactl/media-ioc-enum-entities.md#rst-media-ioc-enum-entities>).

Only forward links that originate at one of the entity's source pads are returned during the enumeration process.





<a id="c-media_links_enum"></a>

```c
media_links_enum
```

<!-- tabularcolumns: |p{4.4cm}|p{4.4cm}|p{8.5cm}| -->


<table>
<caption>struct media_links_enum</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`entity`

</td>
<td>


Entity id, set by the application.

</td>
</tr>
<tr>
<td>


struct `media_pad_desc`

</td>
<td>


\*`pads`

</td>
<td>


Pointer to a pads array allocated by the application. Ignored if NULL.

</td>
</tr>
<tr>
<td>


struct `media_link_desc`

</td>
<td>


\*`links`

</td>
<td>


Pointer to a links array allocated by the application. Ignored if NULL.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved[4]`

</td>
<td>


Reserved for future extensions. Drivers and applications must set the array to zero.

</td>
</tr>
</tbody>
</table>





<a id="c-media_pad_desc"></a>

```c
media_pad_desc
```

<!-- tabularcolumns: |p{4.4cm}|p{4.4cm}|p{8.5cm}| -->


<table>
<caption>struct media_pad_desc</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`entity`

</td>
<td>


ID of the entity this pad belongs to.

</td>
</tr>
<tr>
<td>


\__u16

</td>
<td>


`index`

</td>
<td>


Pad index, starts at 0.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Pad flags, see [media-pad-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-pad-flag>) for more details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved[2]`

</td>
<td>


Reserved for future extensions. Drivers and applications must set the array to zero.

</td>
</tr>
</tbody>
</table>





<a id="c-media_link_desc"></a>

```c
media_link_desc
```

<!-- tabularcolumns: |p{4.4cm}|p{4.4cm}|p{8.5cm}| -->


<table>
<caption>struct media_link_desc</caption>
<tbody>
<tr>
<td>


struct `media_pad_desc`

</td>
<td>


`source`

</td>
<td>


Pad at the origin of this link.

</td>
</tr>
<tr>
<td>


struct `media_pad_desc`

</td>
<td>


`sink`

</td>
<td>


Pad at the target of this link.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Link flags, see [media-link-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-link-flag>) for more details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved[2]`

</td>
<td>


Reserved for future extensions. Drivers and applications must set the array to zero.

</td>
</tr>
</tbody>
</table>

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **EINVAL**

  The struct `media_links_enum` `id` references a non-existing entity.

---

# ioctl MEDIA_IOC_G_TOPOLOGY

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_G_TOPOLOGY - Enumerate the graph topology and graph element properties

<a id="rst-ioctl-media-ioc-g-topology"></a>
<a id="rst-media-ioc-g-topology"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_G_TOPOLOGY - Enumerate the graph topology and graph element properties

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_G_TOPOLOGY"></a>

```c
MEDIA_IOC_G_TOPOLOGY
```

`int ioctl(int fd, MEDIA_IOC_G_TOPOLOGY, struct media_v2_topology *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to struct `media_v2_topology`.

<a id="rst-description"></a>

## Description

The typical usage of this ioctl is to call it twice. On the first call, the structure defined at struct `media_v2_topology` should be zeroed. At return, if no errors happen, this ioctl will return the `topology_version` and the total number of entities, interfaces, pads and links.

Before the second call, the userspace should allocate arrays to store the graph elements that are desired, putting the pointers to them at the ptr_entities, ptr_interfaces, ptr_links and/or ptr_pads, keeping the other values untouched.

If the `topology_version` remains the same, the ioctl should fill the desired arrays with the media graph elements.

<!-- tabularcolumns: |p{1.6cm}|p{3.4cm}|p{12.3cm}| -->





<a id="c-media_v2_topology"></a>

```c
media_v2_topology
```


<table>
<caption>struct media_v2_topology</caption>
<tbody>
<tr>
<td>


\__u64

</td>
<td>


`topology_version`

</td>
<td>


Version of the media graph topology. When the graph is created, this field starts with zero. Every time a graph element is added or removed, this field is incremented.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`num_entities`

</td>
<td>


Number of entities in the graph

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved1`

</td>
<td>


Applications and drivers shall set this to 0.

</td>
</tr>
<tr>
<td>


\__u64

</td>
<td>


`ptr_entities`

</td>
<td>


A pointer to a memory area where the entities array will be stored, converted to a 64-bits integer. It can be zero. if zero, the ioctl won't store the entities. It will just update `num_entities`

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`num_interfaces`

</td>
<td>


Number of interfaces in the graph

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved2`

</td>
<td>


Applications and drivers shall set this to 0.

</td>
</tr>
<tr>
<td>


\__u64

</td>
<td>


`ptr_interfaces`

</td>
<td>


A pointer to a memory area where the interfaces array will be stored, converted to a 64-bits integer. It can be zero. if zero, the ioctl won't store the interfaces. It will just update `num_interfaces`

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`num_pads`

</td>
<td>


Total number of pads in the graph

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved3`

</td>
<td>


Applications and drivers shall set this to 0.

</td>
</tr>
<tr>
<td>


\__u64

</td>
<td>


`ptr_pads`

</td>
<td>


A pointer to a memory area where the pads array will be stored, converted to a 64-bits integer. It can be zero. if zero, the ioctl won't store the pads. It will just update `num_pads`

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`num_links`

</td>
<td>


Total number of data and interface links in the graph

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved4`

</td>
<td>


Applications and drivers shall set this to 0.

</td>
</tr>
<tr>
<td>


\__u64

</td>
<td>


`ptr_links`

</td>
<td>


A pointer to a memory area where the links array will be stored, converted to a 64-bits integer. It can be zero. if zero, the ioctl won't store the links. It will just update `num_links`

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{1.6cm}|p{3.2cm}|p{12.5cm}| -->





<a id="c-media_v2_entity"></a>

```c
media_v2_entity
```


<table>
<caption>struct media_v2_entity</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`id`

</td>
<td>


Unique ID for the entity. Do not expect that the ID will always be the same for each instance of the device. In other words, do not hardcode entity IDs in an application.

</td>
</tr>
<tr>
<td>


char

</td>
<td>


`name`\[64\]

</td>
<td>


Entity name as an UTF-8 NULL-terminated string. This name must be unique within the media topology.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`function`

</td>
<td>


Entity main function, see [media-entity-functions](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-entity-functions>) for details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Entity flags, see [media-entity-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-entity-flag>) for details. Only valid if `MEDIA_V2_ENTITY_HAS_FLAGS(media_version)` returns true. The `media_version` is defined in struct `media_device_info` and can be retrieved using [ioctl MEDIA_IOC_DEVICE_INFO](</kernel/userspace/userspace-api/media-mediactl/media-ioc-device-info.md#rst-media-ioc-device-info>).

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved`\[5\]

</td>
<td>


Reserved for future extensions. Drivers and applications must set this array to zero.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{1.6cm}|p{3.2cm}|p{12.5cm}| -->





<a id="c-media_v2_interface"></a>

```c
media_v2_interface
```


<table>
<caption>struct media_v2_interface</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`id`

</td>
<td>


Unique ID for the interface. Do not expect that the ID will always be the same for each instance of the device. In other words, do not hardcode interface IDs in an application.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`intf_type`

</td>
<td>


Interface type, see [media-intf-type](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-intf-type>) for details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Interface flags. Currently unused.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved`\[9\]

</td>
<td>


Reserved for future extensions. Drivers and applications must set this array to zero.

</td>
</tr>
<tr>
<td>


struct media_v2_intf_devnode

</td>
<td>


`devnode`

</td>
<td>


Used only for device node interfaces. See `media_v2_intf_devnode` for details.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{1.6cm}|p{3.2cm}|p{12.5cm}| -->





<a id="c-media_v2_intf_devnode"></a>

```c
media_v2_intf_devnode
```


<table>
<caption>struct media_v2_intf_devnode</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`major`

</td>
<td>


Device node major number.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`minor`

</td>
<td>


Device node minor number.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{1.6cm}|p{3.2cm}|p{12.5cm}| -->





<a id="c-media_v2_pad"></a>

```c
media_v2_pad
```


<table>
<caption>struct media_v2_pad</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`id`

</td>
<td>


Unique ID for the pad. Do not expect that the ID will always be the same for each instance of the device. In other words, do not hardcode pad IDs in an application.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`entity_id`

</td>
<td>


Unique ID for the entity where this pad belongs.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Pad flags, see [media-pad-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-pad-flag>) for more details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`index`

</td>
<td>


Pad index, starts at 0. Only valid if `MEDIA_V2_PAD_HAS_INDEX(media_version)` returns true. The `media_version` is defined in struct `media_device_info` and can be retrieved using [ioctl MEDIA_IOC_DEVICE_INFO](</kernel/userspace/userspace-api/media-mediactl/media-ioc-device-info.md#rst-media-ioc-device-info>).

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved`\[4\]

</td>
<td>


Reserved for future extensions. Drivers and applications must set this array to zero.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{1.6cm}|p{3.2cm}|p{12.5cm}| -->





<a id="c-media_v2_link"></a>

```c
media_v2_link
```


<table>
<caption>struct media_v2_link</caption>
<tbody>
<tr>
<td>


\__u32

</td>
<td>


`id`

</td>
<td>


Unique ID for the link. Do not expect that the ID will always be the same for each instance of the device. In other words, do not hardcode link IDs in an application.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`source_id`

</td>
<td>


On pad to pad links: unique ID for the source pad.

On interface to entity links: unique ID for the interface.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`sink_id`

</td>
<td>


On pad to pad links: unique ID for the sink pad.

On interface to entity links: unique ID for the entity.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`flags`

</td>
<td>


Link flags, see [media-link-flag](</kernel/userspace/userspace-api/media-mediactl/media-types.md#rst-media-link-flag>) for more details.

</td>
</tr>
<tr>
<td>


\__u32

</td>
<td>


`reserved`\[6\]

</td>
<td>


Reserved for future extensions. Drivers and applications must set this array to zero.

</td>
</tr>
</tbody>
</table>

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **ENOSPC**

  This is returned when either one or more of the num_entities, num_interfaces, num_links or num_pads are non-zero and are smaller than the actual number of elements inside the graph. This may happen if the `topology_version` changed when compared to the last time this ioctl was called. Userspace should usually free the area for the pointers, zero the struct elements and call this ioctl again.

---

# ioctl MEDIA_IOC_REQUEST_ALLOC

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_REQUEST_ALLOC - Allocate a request

<a id="rst-ioctl-media-ioc-request-alloc"></a>
<a id="rst-media-ioc-request-alloc"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_REQUEST_ALLOC - Allocate a request

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_REQUEST_ALLOC"></a>

```c
MEDIA_IOC_REQUEST_ALLOC
```

`int ioctl(int fd, MEDIA_IOC_REQUEST_ALLOC, int *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to an integer.

<a id="rst-description"></a>

## Description

If the media device supports [requests](</kernel/userspace/userspace-api/media-mediactl/request-api.md#rst-media-request-api>), then this ioctl can be used to allocate a request. If it is not supported, then `errno` is set to `ENOTTY`. A request is accessed through a file descriptor that is returned in `*argp`.

If the request was successfully allocated, then the request file descriptor can be passed to the [VIDIOC_QBUF](</kernel/userspace/userspace-api/media-v4l/vidioc-qbuf.md#rst-vidioc-qbuf>), [VIDIOC_G_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>), [VIDIOC_S_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>) and [VIDIOC_TRY_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>) ioctls.

In addition, the request can be queued by calling [ioctl MEDIA_REQUEST_IOC_QUEUE](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-queue.md#rst-media-request-ioc-queue>) and re-initialized by calling [ioctl MEDIA_REQUEST_IOC_REINIT](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-reinit.md#rst-media-request-ioc-reinit>).

Finally, the file descriptor can be [polled](</kernel/userspace/userspace-api/media-mediactl/request-func-poll.md#rst-request-func-poll>) to wait for the request to complete.

The request will remain allocated until all the file descriptors associated with it are closed by `close()` and the driver no longer uses the request internally. See also [here](</kernel/userspace/userspace-api/media-mediactl/request-api.md#rst-media-request-life-time>) for more information.

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **ENOTTY**

  The driver has no support for requests.

---

# ioctl MEDIA_IOC_SETUP_LINK

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_IOC_SETUP_LINK - Modify the properties of a link

<a id="rst-ioctl-media-ioc-setup-link"></a>
<a id="rst-media-ioc-setup-link"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_IOC_SETUP_LINK - Modify the properties of a link

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_IOC_SETUP_LINK"></a>

```c
MEDIA_IOC_SETUP_LINK
```

`int ioctl(int fd, MEDIA_IOC_SETUP_LINK, struct media_link_desc *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by `open()`.

- **`argp`**

  Pointer to struct `media_link_desc`.

<a id="rst-description"></a>

## Description

To change link properties applications fill a struct `media_link_desc` with link identification information (source and sink pad) and the new requested link flags. They then call the MEDIA_IOC_SETUP_LINK ioctl with a pointer to that structure.

The only configurable property is the `ENABLED` link flag to enable/disable a link. Links marked with the `IMMUTABLE` link flag can not be enabled or disabled.

Link configuration has no side effect on other links. If an enabled link at the sink pad prevents the link from being enabled, the driver returns with an `EBUSY` error code.

Only links marked with the `DYNAMIC` link flag can be enabled/disabled while streaming media data. Attempting to enable or disable a streaming non-dynamic link will return an `EBUSY` error code.

If the specified link can't be found the driver returns with an `EINVAL` error code.

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **EINVAL**

  The struct `media_link_desc` references a non-existing link, or the link is immutable and an attempt to modify its configuration was made.

---

# ioctl MEDIA_REQUEST_IOC_QUEUE

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_REQUEST_IOC_QUEUE - Queue a request

<a id="rst-ioctl-media-request-ioc-queue"></a>
<a id="rst-media-request-ioc-queue"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_REQUEST_IOC_QUEUE - Queue a request

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_REQUEST_IOC_QUEUE"></a>

```c
MEDIA_REQUEST_IOC_QUEUE
```

`int ioctl(int request_fd, MEDIA_REQUEST_IOC_QUEUE)`

<a id="rst-arguments"></a>

## Arguments

- **`request_fd`**

  File descriptor returned by [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>).

<a id="rst-description"></a>

## Description

If the media device supports [requests](</kernel/userspace/userspace-api/media-mediactl/request-api.md#rst-media-request-api>), then this request ioctl can be used to queue a previously allocated request.

If the request was successfully queued, then the file descriptor can be [polled](</kernel/userspace/userspace-api/media-mediactl/request-func-poll.md#rst-request-func-poll>) to wait for the request to complete.

If the request was already queued before, then `EBUSY` is returned. Other errors can be returned if the contents of the request contained invalid or inconsistent data, see the next section for a list of common error codes. On error both the request and driver state are unchanged.

Once a request is queued, then the driver is required to gracefully handle errors that occur when the request is applied to the hardware. The exception is the `EIO` error which signals a fatal error that requires the application to stop streaming to reset the hardware state.

It is not allowed to mix queuing requests with queuing buffers directly (without a request). `EBUSY` will be returned if the first buffer was queued directly and you next try to queue a request, or vice versa.

A request must contain at least one buffer, otherwise this ioctl will return an `ENOENT` error.

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

- **EBUSY**

  The request was already queued or the application queued the first buffer directly, but later attempted to use a request. It is not permitted to mix the two APIs.

- **ENOENT**

  The request did not contain any buffers. All requests are required to have at least one buffer. This can also be returned if some required configuration is missing in the request.

- **ENOMEM**

  Out of memory when allocating internal data structures for this request.

- **EINVAL**

  The request has invalid data.

- **EIO**

  The hardware is in a bad state. To recover, the application needs to stop streaming to reset the hardware state and then try to restart streaming.

---

# ioctl MEDIA_REQUEST_IOC_REINIT

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> MEDIA_REQUEST_IOC_REINIT - Re-initialize a request

<a id="rst-ioctl-media-request-ioc-reinit"></a>
<a id="rst-media-request-ioc-reinit"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

MEDIA_REQUEST_IOC_REINIT - Re-initialize a request

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-MEDIA_REQUEST_IOC_REINIT"></a>

```c
MEDIA_REQUEST_IOC_REINIT
```

`int ioctl(int request_fd, MEDIA_REQUEST_IOC_REINIT)`

<a id="rst-arguments"></a>

## Arguments

- **`request_fd`**

  File descriptor returned by [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>).

<a id="rst-description"></a>

## Description

If the media device supports [requests](</kernel/userspace/userspace-api/media-mediactl/request-api.md#rst-media-request-api>), then this request ioctl can be used to re-initialize a previously allocated request.

Re-initializing a request will clear any existing data from the request. This avoids having to `close()` a completed request and allocate a new request. Instead the completed request can just be re-initialized and it is ready to be used again.

A request can only be re-initialized if it either has not been queued yet, or if it was queued and completed. Otherwise it will set `errno` to `EBUSY`. No other error codes can be returned.

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately.

- **EBUSY**

  The request is queued but not yet completed.

---

# Types and flags used to represent the media graph elements

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for every pad.

<a id="rst-types-and-flags-used-to-represent-the-media-graph-elements"></a>
<a id="rst-media-controller-types"></a>

<!-- SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later -->

<!-- tabularcolumns: |p{8.2cm}|p{9.3cm}| -->

<a id="rst-media-ent-f-dv-encoder"></a>
<a id="rst-media-ent-f-dv-decoder"></a>
<a id="rst-media-ent-f-vid-if-bridge"></a>
<a id="rst-media-ent-f-vid-mux"></a>
<a id="rst-media-ent-f-proc-video-isp"></a>
<a id="rst-media-ent-f-proc-video-decoder"></a>
<a id="rst-media-ent-f-proc-video-encoder"></a>
<a id="rst-media-ent-f-proc-video-statistics"></a>
<a id="rst-media-ent-f-proc-video-scaler"></a>
<a id="rst-media-ent-f-proc-video-lut"></a>
<a id="rst-media-ent-f-proc-video-pixel-enc-conv"></a>
<a id="rst-media-ent-f-proc-video-pixel-formatter"></a>
<a id="rst-media-ent-f-proc-video-composer"></a>
<a id="rst-media-ent-f-audio-mixer"></a>
<a id="rst-media-ent-f-audio-playback"></a>
<a id="rst-media-ent-f-audio-capture"></a>
<a id="rst-media-ent-f-if-aud-decoder"></a>
<a id="rst-media-ent-f-if-vid-decoder"></a>
<a id="rst-media-ent-f-tuner"></a>
<a id="rst-media-ent-f-atv-decoder"></a>
<a id="rst-media-ent-f-lens"></a>
<a id="rst-media-ent-f-flash"></a>
<a id="rst-media-ent-f-cam-sensor"></a>
<a id="rst-media-ent-f-conn-composite"></a>
<a id="rst-media-ent-f-conn-svideo"></a>
<a id="rst-media-ent-f-conn-rf"></a>
<a id="rst-media-ent-f-dtv-net-decap"></a>
<a id="rst-media-ent-f-dtv-ca"></a>
<a id="rst-media-ent-f-ts-demux"></a>
<a id="rst-media-ent-f-dtv-demod"></a>
<a id="rst-media-ent-f-io-dtv"></a>
<a id="rst-media-ent-f-io-swradio"></a>
<a id="rst-media-ent-f-io-vbi"></a>
<a id="rst-media-ent-f-io-v4l"></a>
<a id="rst-media-ent-f-v4l2-subdev-unknown"></a>
<a id="rst-media-ent-f-unknown"></a>
<a id="rst-media-entity-functions"></a>


<table>
<caption>Media entity functions</caption>
<tbody>
<tr>
<td>


`MEDIA_ENT_F_UNKNOWN` and `MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN`

</td>
<td>


Unknown entity. That generally indicates that a driver didn't initialize properly the entity, which is a Kernel bug

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IO_V4L`

</td>
<td>


Data streaming input and/or output entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IO_VBI`

</td>
<td>


V4L VBI streaming input or output entity

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IO_SWRADIO`

</td>
<td>


V4L Software Digital Radio (SDR) streaming input or output entity

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IO_DTV`

</td>
<td>


DVB Digital TV streaming input or output entity

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_DTV_DEMOD`

</td>
<td>


Digital TV demodulator entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_TS_DEMUX`

</td>
<td>


MPEG Transport stream demux entity. Could be implemented on hardware or in Kernelspace by the Linux DVB subsystem.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_DTV_CA`

</td>
<td>


Digital TV Conditional Access module (CAM) entity

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_DTV_NET_DECAP`

</td>
<td>


Digital TV network ULE/MLE desencapsulation entity. Could be implemented on hardware or in Kernelspace

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_CONN_RF`

</td>
<td>


Connector for a Radio Frequency (RF) signal.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_CONN_SVIDEO`

</td>
<td>


Connector for a S-Video signal.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_CONN_COMPOSITE`

</td>
<td>


Connector for a RGB composite signal.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_CAM_SENSOR`

</td>
<td>


Camera video sensor entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_FLASH`

</td>
<td>


Flash controller entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_LENS`

</td>
<td>


Lens controller entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_ATV_DECODER`

</td>
<td>


Analog video decoder, the basic function of the video decoder is to accept analogue video from a wide variety of sources such as broadcast, DVD players, cameras and video cassette recorders, in either NTSC, PAL, SECAM or HD format, separating the stream into its component parts, luminance and chrominance, and output it in some digital video standard, with appropriate timing signals.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_TUNER`

</td>
<td>


Digital TV, analog TV, radio and/or software radio tuner, with consists on a PLL tuning stage that converts radio frequency (RF) signal into an Intermediate Frequency (IF). Modern tuners have internally IF-PLL decoders for audio and video, but older models have those stages implemented on separate entities.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IF_VID_DECODER`

</td>
<td>


IF-PLL video decoder. It receives the IF from a PLL and decodes the analog TV video signal. This is commonly found on some very old analog tuners, like Philips MK3 designs. They all contain a tda9887 (or some software compatible similar chip, like tda9885). Those devices use a different I2C address than the tuner PLL.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_IF_AUD_DECODER`

</td>
<td>


IF-PLL sound decoder. It receives the IF from a PLL and decodes the analog TV audio signal. This is commonly found on some very old analog hardware, like Micronas msp3400, Philips tda9840, tda985x, etc. Those devices use a different I2C address than the tuner PLL and should be controlled together with the IF-PLL video decoder.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_AUDIO_CAPTURE`

</td>
<td>


Audio Capture Function Entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_AUDIO_PLAYBACK`

</td>
<td>


Audio Playback Function Entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_AUDIO_MIXER`

</td>
<td>


Audio Mixer Function Entity.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_COMPOSER`

</td>
<td>


Video composer (blender). An entity capable of video composing must have at least two sink pads and one source pad, and composes input video frames onto output video frames. Composition can be performed using alpha blending, color keying, raster operations (ROP), stitching or any other means.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER`

</td>
<td>


Video pixel formatter. An entity capable of pixel formatting must have at least one sink pad and one source pad. Read pixel formatters read pixels from memory and perform a subset of unpacking, cropping, color keying, alpha multiplication and pixel encoding conversion. Write pixel formatters perform a subset of dithering, pixel encoding conversion and packing and write pixels to memory.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV`

</td>
<td>


Video pixel encoding converter. An entity capable of pixel encoding conversion must have at least one sink pad and one source pad, and convert the encoding of pixels received on its sink pad(s) to a different encoding output on its source pad(s). Pixel encoding conversion includes but isn't limited to RGB to/from HSV, RGB to/from YUV and CFA (Bayer) to RGB conversions.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_LUT`

</td>
<td>


Video look-up table. An entity capable of video lookup table processing must have one sink pad and one source pad. It uses the values of the pixels received on its sink pad to look up entries in internal tables and output them on its source pad. The lookup processing can be performed on all components separately or combine them for multi-dimensional table lookups.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_SCALER`

</td>
<td>


Video scaler. An entity capable of video scaling must have at least one sink pad and one source pad, and scale the video frame(s) received on its sink pad(s) to a different resolution output on its source pad(s). The range of supported scaling ratios is entity-specific and can differ between the horizontal and vertical directions (in particular scaling can be supported in one direction only). Binning and sub-sampling (occasionally also referred to as skipping) are considered as scaling.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_STATISTICS`

</td>
<td>


Video statistics computation (histogram, 3A, etc.). An entity capable of statistics computation must have one sink pad and one source pad. It computes statistics over the frames received on its sink pad and outputs the statistics data on its source pad.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_ENCODER`

</td>
<td>


Video (MPEG, HEVC, VPx, etc.) encoder. An entity capable of compressing video frames. Must have one sink pad and at least one source pad.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_DECODER`

</td>
<td>


Video (MPEG, HEVC, VPx, etc.) decoder. An entity capable of decompressing a compressed video stream into uncompressed video frames. Must have one sink pad and at least one source pad.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_PROC_VIDEO_ISP`

</td>
<td>


An Image Signal Processor (ISP) device. ISPs generally are one of a kind devices that have their specific control interfaces using a combination of custom V4L2 controls and IOCTLs, and parameters supplied in a metadata buffer.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_VID_MUX`

</td>
<td>


Video multiplexer. An entity capable of multiplexing must have at least two sink pads and one source pad, and must pass the video frame(s) received from the active sink pad to the source pad.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_VID_IF_BRIDGE`

</td>
<td>


Video interface bridge. A video interface bridge entity must have at least one sink pad and at least one source pad. It receives video frames on its sink pad from an input video bus of one type (HDMI, eDP, MIPI CSI-2, etc.), and outputs them on its source pad to an output video bus of another type (eDP, MIPI CSI-2, parallel, etc.).

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_DV_DECODER`

</td>
<td>


Digital video decoder. The basic function of the video decoder is to accept digital video from a wide variety of sources and output it in some digital video standard, with appropriate timing signals.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_F_DV_ENCODER`

</td>
<td>


Digital video encoder. The basic function of the video encoder is to accept digital video from some digital video standard with appropriate timing signals (usually a parallel video bus with sync signals) and output this to a digital video output connector such as HDMI or DisplayPort.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{5.5cm}|p{12.0cm}| -->

<a id="rst-media-ent-fl-connector"></a>
<a id="rst-media-ent-fl-default"></a>
<a id="rst-media-entity-flag"></a>


<table>
<caption>Media entity flags</caption>
<tbody>
<tr>
<td>


`MEDIA_ENT_FL_DEFAULT`

</td>
<td>


Default entity for its type. Used to discover the default audio, VBI and video devices, the default camera sensor, etc.

</td>
</tr>
<tr>
<td>


`MEDIA_ENT_FL_CONNECTOR`

</td>
<td>


The entity represents a connector.

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{6.5cm}|p{6.0cm}|p{4.8cm}| -->

<a id="rst-media-intf-t-alsa-timer"></a>
<a id="rst-media-intf-t-alsa-sequencer"></a>
<a id="rst-media-intf-t-alsa-hwdep"></a>
<a id="rst-media-intf-t-alsa-rawmidi"></a>
<a id="rst-media-intf-t-alsa-compress"></a>
<a id="rst-media-intf-t-alsa-control"></a>
<a id="rst-media-intf-t-alsa-pcm-playback"></a>
<a id="rst-media-intf-t-alsa-pcm-capture"></a>
<a id="rst-media-intf-t-v4l-touch"></a>
<a id="rst-media-intf-t-v4l-swradio"></a>
<a id="rst-media-intf-t-v4l-subdev"></a>
<a id="rst-media-intf-t-v4l-radio"></a>
<a id="rst-media-intf-t-v4l-vbi"></a>
<a id="rst-media-intf-t-v4l-video"></a>
<a id="rst-media-intf-t-dvb-net"></a>
<a id="rst-media-intf-t-dvb-ca"></a>
<a id="rst-media-intf-t-dvb-dvr"></a>
<a id="rst-media-intf-t-dvb-demux"></a>
<a id="rst-media-intf-t-dvb-fe"></a>
<a id="rst-media-intf-type"></a>


<table>
<caption>Media interface types</caption>
<tbody>
<tr>
<td>


`MEDIA_INTF_T_DVB_FE`

</td>
<td>


Device node interface for the Digital TV frontend

</td>
<td>


typically, /dev/dvb/adapter?/frontend?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_DVB_DEMUX`

</td>
<td>


Device node interface for the Digital TV demux

</td>
<td>


typically, /dev/dvb/adapter?/demux?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_DVB_DVR`

</td>
<td>


Device node interface for the Digital TV DVR

</td>
<td>


typically, /dev/dvb/adapter?/dvr?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_DVB_CA`

</td>
<td>


Device node interface for the Digital TV Conditional Access

</td>
<td>


typically, /dev/dvb/adapter?/ca?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_DVB_NET`

</td>
<td>


Device node interface for the Digital TV network control

</td>
<td>


typically, /dev/dvb/adapter?/net?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_VIDEO`

</td>
<td>


Device node interface for video (V4L)

</td>
<td>


typically, /dev/video?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_VBI`

</td>
<td>


Device node interface for VBI (V4L)

</td>
<td>


typically, /dev/vbi?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_RADIO`

</td>
<td>


Device node interface for radio (V4L)

</td>
<td>


typically, /dev/radio?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_SUBDEV`

</td>
<td>


Device node interface for a V4L subdevice

</td>
<td>


typically, /dev/v4l-subdev?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_SWRADIO`

</td>
<td>


Device node interface for Software Defined Radio (V4L)

</td>
<td>


typically, /dev/swradio?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_V4L_TOUCH`

</td>
<td>


Device node interface for Touch device (V4L)

</td>
<td>


typically, /dev/v4l-touch?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_PCM_CAPTURE`

</td>
<td>


Device node interface for ALSA PCM Capture

</td>
<td>


typically, /dev/snd/pcmC?D?c

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_PCM_PLAYBACK`

</td>
<td>


Device node interface for ALSA PCM Playback

</td>
<td>


typically, /dev/snd/pcmC?D?p

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_CONTROL`

</td>
<td>


Device node interface for ALSA Control

</td>
<td>


typically, /dev/snd/controlC?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_COMPRESS`

</td>
<td>


Device node interface for ALSA Compress

</td>
<td>


typically, /dev/snd/compr?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_RAWMIDI`

</td>
<td>


Device node interface for ALSA Raw MIDI

</td>
<td>


typically, /dev/snd/midi?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_HWDEP`

</td>
<td>


Device node interface for ALSA Hardware Dependent

</td>
<td>


typically, /dev/snd/hwC?D?

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_SEQUENCER`

</td>
<td>


Device node interface for ALSA Sequencer

</td>
<td>


typically, /dev/snd/seq

</td>
</tr>
<tr>
<td>


`MEDIA_INTF_T_ALSA_TIMER`

</td>
<td>


Device node interface for ALSA Timer

</td>
<td>


typically, /dev/snd/timer

</td>
</tr>
</tbody>
</table>

<!-- tabularcolumns: |p{5.5cm}|p{12.0cm}| -->

<a id="rst-media-pad-fl-must-connect"></a>
<a id="rst-media-pad-fl-source"></a>
<a id="rst-media-pad-fl-sink"></a>
<a id="rst-media-pad-flag"></a>


<table>
<caption>Media pad flags</caption>
<tbody>
<tr>
<td>


`MEDIA_PAD_FL_SINK`

</td>
<td>


Input pad, relative to the entity. Input pads sink data and are targets of links.

</td>
</tr>
<tr>
<td>


`MEDIA_PAD_FL_SOURCE`

</td>
<td>


Output pad, relative to the entity. Output pads source data and are origins of links.

</td>
</tr>
<tr>
<td>


`MEDIA_PAD_FL_MUST_CONNECT`

</td>
<td>


If this flag is set, then for this pad to be able to stream, it must be connected by at least one enabled link. There could be temporary reasons (e.g. device configuration dependent) for the pad to need enabled links even when this flag isn't set; the absence of the flag doesn't imply there is none.

</td>
</tr>
</tbody>
</table>

One and only one of `MEDIA_PAD_FL_SINK` and `MEDIA_PAD_FL_SOURCE` must be set for every pad.

<!-- tabularcolumns: |p{5.5cm}|p{12.0cm}| -->

<a id="rst-media-lnk-fl-link-type"></a>
<a id="rst-media-lnk-fl-dynamic"></a>
<a id="rst-media-lnk-fl-immutable"></a>
<a id="rst-media-lnk-fl-enabled"></a>
<a id="rst-media-link-flag"></a>


<table>
<caption>Media link flags</caption>
<tbody>
<tr>
<td>


`MEDIA_LNK_FL_ENABLED`

</td>
<td>


The link is enabled and can be used to transfer media data. When two or more links target a sink pad, only one of them can be enabled at a time.

</td>
</tr>
<tr>
<td>


`MEDIA_LNK_FL_IMMUTABLE`

</td>
<td>


The link enabled state can't be modified at runtime. An immutable link is always enabled.

</td>
</tr>
<tr>
<td>


`MEDIA_LNK_FL_DYNAMIC`

</td>
<td>


The link enabled state can be modified during streaming. This flag is set by drivers and is read-only for applications.

</td>
</tr>
<tr>
<td>


`MEDIA_LNK_FL_LINK_TYPE`

</td>
<td>


This is a bitmask that defines the type of the link. The following link types are currently supported:

<a id="rst-media-lnk-fl-data-link"></a>

`MEDIA_LNK_FL_DATA_LINK` for links that represent a data connection between two pads.

<a id="rst-media-lnk-fl-interface-link"></a>

`MEDIA_LNK_FL_INTERFACE_LINK` for links that associate an entity to its interface.

<a id="rst-media-lnk-fl-ancillary-link"></a>

`MEDIA_LNK_FL_ANCILLARY_LINK` for links that represent a physical relationship between two entities. The link may or may not be immutable, so applications must not assume either case.

</td>
</tr>
</tbody>
</table>

---

# Request API

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> The Request API has been designed to allow V4L2 to deal with requirements of modern devices (stateless codecs, complex camera pipelines, ...) and APIs (Android Codec v2). One such requirement is the ability for devices belonging to the…

<a id="rst-request-api"></a>
<a id="rst-media-request-api"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

The Request API has been designed to allow V4L2 to deal with requirements of modern devices (stateless codecs, complex camera pipelines, ...) and APIs (Android Codec v2). One such requirement is the ability for devices belonging to the same pipeline to reconfigure and collaborate closely on a per-frame basis. Another is support of stateless codecs, which require controls to be applied to specific frames (aka 'per-frame controls') in order to be used efficiently.

While the initial use-case was V4L2, it can be extended to other subsystems as well, as long as they use the media controller.

Supporting these features without the Request API is not always possible and if it is, it is terribly inefficient: user-space would have to flush all activity on the media pipeline, reconfigure it for the next frame, queue the buffers to be processed with that configuration, and wait until they are all available for dequeuing before considering the next frame. This defeats the purpose of having buffer queues since in practice only one buffer would be queued at a time.

The Request API allows a specific configuration of the pipeline (media controller topology + configuration for each media entity) to be associated with specific buffers. This allows user-space to schedule several tasks ("requests") with different configurations in advance, knowing that the configuration will be applied when needed to get the expected result. Configuration values at the time of request completion are also available for reading.

<a id="rst-general-usage"></a>

## General Usage

The Request API extends the Media Controller API and cooperates with subsystem-specific APIs to support request usage. At the Media Controller level, requests are allocated from the supporting Media Controller device node. Their life cycle is then managed through the request file descriptors in an opaque way. Configuration data, buffer handles and processing results stored in requests are accessed through subsystem-specific APIs extended for request support, such as V4L2 APIs that take an explicit `request_fd` parameter.

<a id="rst-request-allocation"></a>

## Request Allocation

User-space allocates requests using [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>) for the media device node. This returns a file descriptor representing the request. Typically, several such requests will be allocated.

<a id="rst-request-preparation"></a>

## Request Preparation

Standard V4L2 ioctls can then receive a request file descriptor to express the fact that the ioctl is part of said request, and is not to be applied immediately. See [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>) for a list of ioctls that support this. Configurations set with a `request_fd` parameter are stored instead of being immediately applied, and buffers queued to a request do not enter the regular buffer queue until the request itself is queued.

<a id="rst-request-submission"></a>

## Request Submission

Once the configuration and buffers of the request are specified, it can be queued by calling [ioctl MEDIA_REQUEST_IOC_QUEUE](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-queue.md#rst-media-request-ioc-queue>) on the request file descriptor. A request must contain at least one buffer, otherwise `ENOENT` is returned. A queued request cannot be modified anymore.

> [!CAUTION]
>
> For [memory-to-memory devices](</kernel/userspace/userspace-api/media-v4l/dev-mem2mem.md#rst-mem2mem>) you can use requests only for output buffers, not for capture buffers. Attempting to add a capture buffer to a request will result in an `EBADR` error.

If the request contains configurations for multiple entities, individual drivers may synchronize so the requested pipeline's topology is applied before the buffers are processed. Media controller drivers do a best effort implementation since perfect atomicity may not be possible due to hardware limitations.

> [!CAUTION]
>
> It is not allowed to mix queuing requests with directly queuing buffers: whichever method is used first locks this in place until [VIDIOC_STREAMOFF](</kernel/userspace/userspace-api/media-v4l/vidioc-streamon.md#rst-vidioc-streamon>) is called or the device is [closed](</kernel/userspace/userspace-api/media-v4l/func-close.md#rst-func-close>). Attempts to directly queue a buffer when earlier a buffer was queued via a request or vice versa will result in an `EBUSY` error.

Controls can still be set without a request and are applied immediately, regardless of whether a request is in use or not.

> [!CAUTION]
>
> Setting the same control through a request and also directly can lead to undefined behavior\!

User-space can `poll()` a request file descriptor in order to wait until the request completes. A request is considered complete once all its associated buffers are available for dequeuing and all the associated controls have been updated with the values at the time of completion. Note that user-space does not need to wait for the request to complete to dequeue its buffers: buffers that are available halfway through a request can be dequeued independently of the request's state.

A completed request contains the state of the device after the request was executed. User-space can query that state by calling [ioctl VIDIOC_G_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>) with the request file descriptor. Calling [ioctl VIDIOC_G_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>) for a request that has been queued but not yet completed will return `EBUSY` since the control values might be changed at any time by the driver while the request is in flight.

<a id="rst-recycling-and-destruction"></a>
<a id="rst-media-request-life-time"></a>

## Recycling and Destruction

Finally, a completed request can either be discarded or be reused. Calling `close()` on a request file descriptor will make that file descriptor unusable and the request will be freed once it is no longer in use by the kernel. That is, if the request is queued and then the file descriptor is closed, then it won't be freed until the driver completed the request.

The [ioctl MEDIA_REQUEST_IOC_REINIT](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-reinit.md#rst-media-request-ioc-reinit>) will clear a request's state and make it available again. No state is retained by this operation: the request is as if it had just been allocated.

<a id="rst-example-for-a-codec-device"></a>

## Example for a Codec Device

For use-cases such as [codecs](</kernel/userspace/userspace-api/media-v4l/dev-mem2mem.md#rst-mem2mem>), the request API can be used to associate specific controls to be applied by the driver for the OUTPUT buffer, allowing user-space to queue many such buffers in advance. It can also take advantage of requests' ability to capture the state of controls when the request completes to read back information that may be subject to change.

Put into code, after obtaining a request, user-space can assign controls and one OUTPUT buffer to it:



```c
struct v4l2_buffer buf;
struct v4l2_ext_controls ctrls;
int req_fd;
...
if (ioctl(media_fd, MEDIA_IOC_REQUEST_ALLOC, &req_fd))
        return errno;
...
ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
ctrls.request_fd = req_fd;
if (ioctl(codec_fd, VIDIOC_S_EXT_CTRLS, &ctrls))
        return errno;
...
buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buf.flags |= V4L2_BUF_FLAG_REQUEST_FD;
buf.request_fd = req_fd;
if (ioctl(codec_fd, VIDIOC_QBUF, &buf))
        return errno;
```

Note that it is not allowed to use the Request API for CAPTURE buffers since there are no per-frame settings to report there.

Once the request is fully prepared, it can be queued to the driver:



```c
if (ioctl(req_fd, MEDIA_REQUEST_IOC_QUEUE))
        return errno;
```

User-space can then either wait for the request to complete by calling poll() on its file descriptor, or start dequeuing CAPTURE buffers. Most likely, it will want to get CAPTURE buffers as soon as possible and this can be done using a regular [VIDIOC_DQBUF](</kernel/userspace/userspace-api/media-v4l/vidioc-qbuf.md#rst-vidioc-qbuf>):



```c
struct v4l2_buffer buf;

memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(codec_fd, VIDIOC_DQBUF, &buf))
        return errno;
```

Note that this example assumes for simplicity that for every OUTPUT buffer there will be one CAPTURE buffer, but this does not have to be the case.

We can then, after ensuring that the request is completed via polling the request file descriptor, query control values at the time of its completion via a call to [VIDIOC_G_EXT_CTRLS](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>). This is particularly useful for volatile controls for which we want to query values as soon as the capture buffer is produced.



```c
struct pollfd pfd = { .events = POLLPRI, .fd = req_fd };
poll(&pfd, 1, -1);
...
ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
ctrls.request_fd = req_fd;
if (ioctl(codec_fd, VIDIOC_G_EXT_CTRLS, &ctrls))
        return errno;
```

Once we don't need the request anymore, we can either recycle it for reuse with [ioctl MEDIA_REQUEST_IOC_REINIT](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-reinit.md#rst-media-request-ioc-reinit>)...



```c
if (ioctl(req_fd, MEDIA_REQUEST_IOC_REINIT))
        return errno;
```

... or close its file descriptor to completely dispose of it.



```c
close(req_fd);
```

<a id="rst-example-for-a-simple-capture-device"></a>

## Example for a Simple Capture Device

With a simple capture device, requests can be used to specify controls to apply for a given CAPTURE buffer.



```c
struct v4l2_buffer buf;
struct v4l2_ext_controls ctrls;
int req_fd;
...
if (ioctl(media_fd, MEDIA_IOC_REQUEST_ALLOC, &req_fd))
        return errno;
...
ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
ctrls.request_fd = req_fd;
if (ioctl(camera_fd, VIDIOC_S_EXT_CTRLS, &ctrls))
        return errno;
...
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.flags |= V4L2_BUF_FLAG_REQUEST_FD;
buf.request_fd = req_fd;
if (ioctl(camera_fd, VIDIOC_QBUF, &buf))
        return errno;
```

Once the request is fully prepared, it can be queued to the driver:



```c
if (ioctl(req_fd, MEDIA_REQUEST_IOC_QUEUE))
        return errno;
```

User-space can then dequeue buffers, wait for the request completion, query controls and recycle the request as in the M2M example above.

---

# request close()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> request-close - Close a request file descriptor

<a id="rst-request-close"></a>
<a id="rst-request-func-close"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC.request -->

<a id="rst-name"></a>

## Name

request-close - Close a request file descriptor

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <unistd.h>
```





<a id="c-close"></a>

```c
int close( int fd )
```

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>).

<a id="rst-description"></a>

## Description

Closes the request file descriptor. Resources associated with the request are freed once all file descriptors associated with the request are closed and the driver has completed the request. See [here](</kernel/userspace/userspace-api/media-mediactl/request-api.md#rst-media-request-life-time>) for more information.

<a id="rst-return-value"></a>

## Return Value

`close()` returns 0 on success. On error, -1 is returned, and `errno` is set appropriately. Possible error codes are:

- **EBADF**

  `fd` is not a valid open file descriptor.

---

# request ioctl()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> request-ioctl - Control a request file descriptor

<a id="rst-request-ioctl"></a>
<a id="rst-request-func-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

request-ioctl - Control a request file descriptor

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <sys/ioctl.h>
```

`int ioctl(int fd, int cmd, void *argp)`

<a id="rst-arguments"></a>

## Arguments

- **`fd`**

  File descriptor returned by [ioctl MEDIA_IOC_REQUEST_ALLOC](</kernel/userspace/userspace-api/media-mediactl/media-ioc-request-alloc.md#rst-media-ioc-request-alloc>).

- **`cmd`**

  The request ioctl command code as defined in the media.h header file, for example [ioctl MEDIA_REQUEST_IOC_QUEUE](</kernel/userspace/userspace-api/media-mediactl/media-request-ioc-queue.md#rst-media-request-ioc-queue>).

- **`argp`**

  Pointer to a request-specific structure.

<a id="rst-description"></a>

## Description

The [ioctl()](<#rst-request-func-ioctl>) function manipulates request parameters. The argument `fd` must be an open file descriptor.

The ioctl `cmd` code specifies the request function to be called. It has encoded in it whether the argument is an input, output or read/write parameter, and the size of the argument `argp` in bytes.

Macros and structures definitions specifying request ioctl commands and their parameters are located in the media.h header file. All request ioctl commands, their respective function and parameters are specified in [Function Reference](</kernel/userspace/userspace-api/media-mediactl/media-funcs.md#rst-media-user-func>).

<a id="rst-return-value"></a>

## Return Value

On success 0 is returned, on error -1 and the `errno` variable is set appropriately. The generic error codes are described at the [Generic Error Codes](</kernel/userspace/userspace-api/media/gen-errors.md#rst-gen-errors-1>) chapter.

Command-specific error codes are listed in the individual command descriptions.

When an ioctl that takes an output or read/write parameter fails, the parameter remains unmodified.

---

# request poll()

_Peios Kernel / Userspace interfaces / System and device interfaces / Media controllers_

> request-poll - Wait for some event on a file descriptor

<a id="rst-request-poll"></a>
<a id="rst-request-func-poll"></a>

<!-- SPDX-License-Identifier: GPL-2.0 OR GFDL-1.1-no-invariants-or-later -->

<!-- c:namespace: MC -->

<a id="rst-name"></a>

## Name

request-poll - Wait for some event on a file descriptor

<a id="rst-synopsis"></a>

## Synopsis



```c
#include <sys/poll.h>
```





<a id="c-poll"></a>

```c
int poll( struct pollfd *ufds, unsigned int nfds, int timeout )
```

<a id="rst-arguments"></a>

## Arguments

- **`ufds`**

  List of file descriptor events to be watched

- **`nfds`**

  Number of file descriptor events at the \*ufds array

- **`timeout`**

  Timeout to wait for events

<a id="rst-description"></a>

## Description

With the `poll()` function applications can wait for a request to complete.

On success `poll()` returns the number of file descriptors that have been selected (that is, file descriptors for which the `revents` field of the respective struct `pollfd` is non-zero). Request file descriptor set the `POLLPRI` flag in `revents` when the request was completed.  When the function times out it returns a value of zero, on failure it returns -1 and the `errno` variable is set appropriately.

Attempting to poll for a request that is not yet queued will set the `POLLERR` flag in `revents`.

<a id="rst-return-value"></a>

## Return Value

On success, `poll()` returns the number of structures which have non-zero `revents` fields, or zero if the call timed out. On error -1 is returned, and the `errno` variable is set appropriately:

- **`EBADF`**

  One or more of the `ufds` members specify an invalid file descriptor.

- **`EFAULT`**

  `ufds` references an inaccessible memory area.

- **`EINTR`**

  The call was interrupted by a signal.

- **`EINVAL`**

  The `nfds` value exceeds the `RLIMIT_NOFILE` value. Use `getrlimit()` to obtain this value.
