← Documents Documentation/userspace-api/media/v4l/vidioc-expbuf.rst GitHub 원문 ↗

Linux 6.18.37 · 사용자 공간 API

VIDIOC_EXPBUF ioctl

V4L2 MMAP 버퍼의 single-planar·multi-planar plane을 DMABUF fd로 내보내는 절차, 허용 플래그, 구조체 필드와 정리·오류 규칙을 설명합니다.

Source pathDocumentation/userspace-api/media/v4l/vidioc-expbuf.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

vidioc-expbuf.rst:1-162

V4L2 MMAP 버퍼의 single-planar·multi-planar plane을 DMABUF fd로 내보내는 절차, 허용 플래그, 구조체 필드와 정리·오류 규칙을 설명합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2 .. c:namespace:: V4L
3
4 .. _VIDIOC_EXPBUF:
5
6 *******************
7 ioctl VIDIOC_EXPBUF
8 *******************
9
10 Name
11 ====
12
13 VIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
14
15 Synopsis
16 ========
17
18 .. c:macro:: VIDIOC_EXPBUF
19
20 ``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)``
21
22 Arguments
23 =========
24
25 ``fd``
26 File descriptor returned by :c:func:`open()`.
27
28 ``argp``
29 Pointer to struct :c:type:`v4l2_exportbuffer`.
30
31 Description
32 ===========
33
34 This ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
35 method, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
36 It can be used to export a buffer as a DMABUF file at any time after
37 buffers have been allocated with the
38 :ref:`VIDIOC_REQBUFS` ioctl.
39
40 To export a buffer, applications fill struct
41 :c:type:`v4l2_exportbuffer`. The ``type`` field is
42 set to the same buffer type as was previously used with struct
43 :c:type:`v4l2_requestbuffers` ``type``.
44 Applications must also set the ``index`` field. Valid index numbers
45 range from zero to the number of buffers allocated with
46 :ref:`VIDIOC_REQBUFS` (struct
47 :c:type:`v4l2_requestbuffers` ``count``) minus
48 one. For the multi-planar API, applications set the ``plane`` field to
49 the index of the plane to be exported. Valid planes range from zero to
50 the maximal number of valid planes for the currently active format. For
51 the single-planar API, applications must set ``plane`` to zero.
52 Additional flags may be posted in the ``flags`` field. Refer to a manual
53 for open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
54 and O_RDWR are supported. All other fields must be set to zero. In the
55 case of multi-planar API, every plane is exported separately using
56 multiple :ref:`VIDIOC_EXPBUF` calls.
57
58 After calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
59 driver. This is a DMABUF file descriptor. The application may pass it to
60 other DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
61 for details about importing DMABUF files into V4L2 nodes. It is
62 recommended to close a DMABUF file when it is no longer used to allow
63 the associated memory to be reclaimed.
64
65 Examples
66 ========
67
68 .. code-block:: c
69
70 int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
71 {
72 struct v4l2_exportbuffer expbuf;
73
74 memset(&expbuf, 0, sizeof(expbuf));
75 expbuf.type = bt;
76 expbuf.index = index;
77 if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
78 perror("VIDIOC_EXPBUF");
79 return -1;
80 }
81
82 *dmafd = expbuf.fd;
83
84 return 0;
85 }
86
87 .. code-block:: c
88
89 int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
90 int dmafd[], int n_planes)
91 {
92 int i;
93
94 for (i = 0; i < n_planes; ++i) {
95 struct v4l2_exportbuffer expbuf;
96
97 memset(&expbuf, 0, sizeof(expbuf));
98 expbuf.type = bt;
99 expbuf.index = index;
100 expbuf.plane = i;
101 if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
102 perror("VIDIOC_EXPBUF");
103 while (i)
104 close(dmafd[--i]);
105 return -1;
106 }
107 dmafd[i] = expbuf.fd;
108 }
109
110 return 0;
111 }
112
113 .. c:type:: v4l2_exportbuffer
114
115 .. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
116
117 .. flat-table:: struct v4l2_exportbuffer
118 :header-rows: 0
119 :stub-columns: 0
120 :widths: 1 1 2
121
122 * - __u32
123 - ``type``
124 - Type of the buffer, same as struct
125 :c:type:`v4l2_format` ``type`` or struct
126 :c:type:`v4l2_requestbuffers` ``type``, set
127 by the application. See :c:type:`v4l2_buf_type`
128 * - __u32
129 - ``index``
130 - Number of the buffer, set by the application. This field is only
131 used for :ref:`memory mapping <mmap>` I/O and can range from
132 zero to the number of buffers allocated with the
133 :ref:`VIDIOC_REQBUFS` and/or
134 :ref:`VIDIOC_CREATE_BUFS` ioctls.
135 * - __u32
136 - ``plane``
137 - Index of the plane to be exported when using the multi-planar API.
138 Otherwise this value must be set to zero.
139 * - __u32
140 - ``flags``
141 - Flags for the newly created file, currently only ``O_CLOEXEC``,
142 ``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
143 the manual of open() for more details.
144 * - __s32
145 - ``fd``
146 - The DMABUF file descriptor associated with a buffer. Set by the
147 driver.
148 * - __u32
149 - ``reserved[11]``
150 - Reserved field for future use. Drivers and applications must set
151 the array to zero.
152
153 Return Value
154 ============
155
156 On success 0 is returned, on error -1 and the ``errno`` variable is set
157 appropriately. The generic error codes are described at the
158 :ref:`Generic Error Codes <gen-errors>` chapter.
159
160 EINVAL
161 A queue is not in MMAP mode or DMABUF exporting is not supported or
162 ``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.
163

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

목적, 호출 형식과 인자

1-30

`VIDIOC_EXPBUF`는 V4L2 버퍼를 DMABUF 파일 디스크립터로 내보내는 ioctl입니다. 호출 형식은 `int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)`입니다.

`fd`는 `open()`이 반환한 V4L2 장치 파일 디스크립터이고, `argp`는 내보낼 버퍼·plane·파일 플래그와 반환되는 DMABUF fd를 담는 `struct v4l2_exportbuffer`를 가리킵니다.

.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
.. c:namespace:: V4L

.. _VIDIOC_EXPBUF:

*******************
ioctl VIDIOC_EXPBUF
*******************

Name
====

VIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.

Synopsis
========

.. c:macro:: VIDIOC_EXPBUF

``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)``

Arguments
=========

``fd``
    File descriptor returned by :c:func:`open()`.

``argp``
    Pointer to struct :c:type:`v4l2_exportbuffer`.

DMABUF 내보내기 절차와 제약

31-64

이 ioctl은 memory mapping I/O 방식의 확장이므로 `V4L2_MEMORY_MMAP` 버퍼에만 사용할 수 있습니다. `VIDIOC_REQBUFS`로 버퍼를 할당한 뒤에는 언제든 해당 버퍼를 DMABUF 파일로 내보낼 수 있습니다.

응용 프로그램은 `v4l2_exportbuffer.type`을 앞서 `v4l2_requestbuffers.type`에 사용한 것과 같은 buffer type으로 설정합니다. `index`는 0부터 `VIDIOC_REQBUFS`가 반환한 `count - 1`까지의 할당된 버퍼 번호입니다.

multi-planar API에서는 현재 활성 format이 허용하는 plane 범위 안에서 내보낼 `plane` index를 설정합니다. 각 plane은 별도의 `VIDIOC_EXPBUF` 호출로 하나씩 내보내야 합니다.

single-planar API에서는 `plane`을 반드시 0으로 설정합니다. 이 차이를 무시하면 `EINVAL`이 반환됩니다.

`flags`에는 새 DMABUF 파일에 적용할 `O_CLOEXEC`, `O_RDONLY`, `O_WRONLY`, `O_RDWR`만 사용할 수 있습니다. 자세한 의미는 `open()` 매뉴얼을 따르며, 그 밖의 필드는 모두 0으로 초기화해야 합니다.

호출이 성공하면 드라이버가 `fd` 필드에 DMABUF 파일 디스크립터를 기록합니다. 응용 프로그램은 이를 다른 DMABUF 인식 장치에 전달하거나 V4L2 DMABUF importing 절차에 사용할 수 있습니다.

DMABUF 파일을 더 이상 사용하지 않을 때는 연결된 메모리를 회수할 수 있도록 fd를 닫는 것이 권장됩니다.

V4L2 버퍼를 DMABUF로 내보내기
VIDIOC_REQBUFS로 MMAP 버퍼 할당구조체 전체를 0으로 초기화type·index·plane·flags 설정VIDIOC_EXPBUF 호출반환된 DMABUF fd 전달 또는 사용사용 종료 후 fd 닫기

MMAP 버퍼의 plane을 파일 디스크립터로 변환하는 순서입니다.

Description
===========

This ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
method, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
It can be used to export a buffer as a DMABUF file at any time after
buffers have been allocated with the
:ref:`VIDIOC_REQBUFS` ioctl.

To export a buffer, applications fill struct
:c:type:`v4l2_exportbuffer`. The ``type`` field is
set to the same buffer type as was previously used with struct
:c:type:`v4l2_requestbuffers` ``type``.
Applications must also set the ``index`` field. Valid index numbers
range from zero to the number of buffers allocated with
:ref:`VIDIOC_REQBUFS` (struct
:c:type:`v4l2_requestbuffers` ``count``) minus
one. For the multi-planar API, applications set the ``plane`` field to
the index of the plane to be exported. Valid planes range from zero to
the maximal number of valid planes for the currently active format. For
the single-planar API, applications must set ``plane`` to zero.
Additional flags may be posted in the ``flags`` field. Refer to a manual
for open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
and O_RDWR are supported. All other fields must be set to zero. In the
case of multi-planar API, every plane is exported separately using
multiple :ref:`VIDIOC_EXPBUF` calls.

After calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
driver. This is a DMABUF file descriptor. The application may pass it to
other DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
for details about importing DMABUF files into V4L2 nodes. It is
recommended to close a DMABUF file when it is no longer used to allow
the associated memory to be reclaimed.

single-planar와 multi-planar 예제

65-112

`buffer_export()` 예제는 `v4l2_exportbuffer` 전체를 `memset()`으로 0으로 만든 뒤 `type`과 `index`를 설정합니다. ioctl이 실패하면 오류를 출력하고 -1을 반환하며, 성공하면 `expbuf.fd`를 호출자에게 돌려줍니다.

`buffer_export_mp()`는 `n_planes`만큼 반복하면서 매 plane마다 새 구조체를 0으로 초기화하고 `type`, `index`, `plane`을 설정해 각각 내보냅니다.

multi-planar 예제에서 중간 plane의 ioctl이 실패하면 `while (i) close(dmafd[--i])`로 앞서 성공한 모든 DMABUF fd를 닫은 뒤 -1을 반환합니다. 모든 plane이 성공하면 fd 배열을 유지하고 0을 반환합니다.

Examples
========

.. code-block:: c

    int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
    {
	struct v4l2_exportbuffer expbuf;

	memset(&expbuf, 0, sizeof(expbuf));
	expbuf.type = bt;
	expbuf.index = index;
	if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
	    perror("VIDIOC_EXPBUF");
	    return -1;
	}

	*dmafd = expbuf.fd;

	return 0;
    }

.. code-block:: c

    int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
	int dmafd[], int n_planes)
    {
	int i;

	for (i = 0; i < n_planes; ++i) {
	    struct v4l2_exportbuffer expbuf;

	    memset(&expbuf, 0, sizeof(expbuf));
	    expbuf.type = bt;
	    expbuf.index = index;
	    expbuf.plane = i;
	    if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
		perror("VIDIOC_EXPBUF");
		while (i)
		    close(dmafd[--i]);
		return -1;
	    }
	    dmafd[i] = expbuf.fd;
	}

	return 0;
    }

v4l2_exportbuffer 구조체

113-152
struct v4l2_exportbuffer
형식필드의미
`__u32``type`응용 프로그램이 설정하는 buffer type. `v4l2_format.type` 또는 `v4l2_requestbuffers.type`과 같음
`__u32``index`응용 프로그램이 설정하는 MMAP 버퍼 번호. `VIDIOC_REQBUFS` 및/또는 `VIDIOC_CREATE_BUFS`로 할당된 범위 안이어야 함
`__u32``plane`multi-planar API에서 내보낼 plane index. 그 밖에는 0
`__u32``flags`새 파일의 플래그. `O_CLOEXEC`, `O_RDONLY`, `O_WRONLY`, `O_RDWR`만 지원
`__s32``fd`버퍼에 연결된 DMABUF 파일 디스크립터. 드라이버가 설정
`__u32[11]``reserved`미래 확장용. 드라이버와 응용 프로그램 모두 배열 전체를 0으로 설정

응용 프로그램 입력과 드라이버 반환 필드를 원문 순서대로 정리합니다.

`type`은 `enum v4l2_buf_type` 값이며, 버퍼를 만들고 format을 설정할 때 사용한 queue type과 일치해야 합니다.

`index`는 MMAP I/O에서만 사용됩니다. 유효 범위는 `VIDIOC_REQBUFS`뿐 아니라 필요에 따라 `VIDIOC_CREATE_BUFS`로 추가 할당한 버퍼까지 포함합니다.

`fd`는 입력값이 아니라 드라이버의 출력값입니다. 반면 `reserved[11]`은 양쪽 모두 0으로 유지해야 ABI의 미래 확장과 호환됩니다.

.. c:type:: v4l2_exportbuffer

.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|

.. flat-table:: struct v4l2_exportbuffer
    :header-rows:  0
    :stub-columns: 0
    :widths:       1 1 2

    * - __u32
      - ``type``
      - Type of the buffer, same as struct
	:c:type:`v4l2_format` ``type`` or struct
	:c:type:`v4l2_requestbuffers` ``type``, set
	by the application. See :c:type:`v4l2_buf_type`
    * - __u32
      - ``index``
      - Number of the buffer, set by the application. This field is only
	used for :ref:`memory mapping <mmap>` I/O and can range from
	zero to the number of buffers allocated with the
	:ref:`VIDIOC_REQBUFS` and/or
	:ref:`VIDIOC_CREATE_BUFS` ioctls.
    * - __u32
      - ``plane``
      - Index of the plane to be exported when using the multi-planar API.
	Otherwise this value must be set to zero.
    * - __u32
      - ``flags``
      - Flags for the newly created file, currently only ``O_CLOEXEC``,
	``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
	the manual of open() for more details.
    * - __s32
      - ``fd``
      - The DMABUF file descriptor associated with a buffer. Set by the
	driver.
    * - __u32
      - ``reserved[11]``
      - Reserved field for future use. Drivers and applications must set
	the array to zero.

반환값과 EINVAL

153-162

성공하면 0을 반환합니다. 오류가 발생하면 -1을 반환하고 `errno`를 적절한 값으로 설정하며, 공통 오류 코드는 Generic Error Codes 장을 따릅니다.

VIDIOC_EXPBUF 오류
errno조건
`EINVAL`queue가 MMAP mode가 아니거나 DMABUF export를 지원하지 않거나 `flags`, `type`, `index`, `plane` 중 하나가 유효하지 않음

MMAP 상태, 기능 지원 또는 구조체 필드가 잘못된 경우입니다.

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
:ref:`Generic Error Codes <gen-errors>` chapter.

EINVAL
    A queue is not in MMAP mode or DMABUF exporting is not supported or
    ``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.