Documentation/driver-api/media/v4l2-fh.rst GitHub 원문 ↗

Linux 6.18.37 · Driver API

V4L2 File handles

v4l2_fh의 필수 open·release 생명주기와 helper를 설명하는 전문 번역입니다.

Source pathDocumentation/driver-api/media/v4l2-fh.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

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

1. 요약·해설

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

요약과 해설

v4l2-fh.rst:1-136

`v4l2_fh`는 모든 V4L2 driver가 사용해야 하는 file handle별 상태 container이며, init·add와 del·exit 순서를 대칭으로 지켜야 합니다.

문서 구성
원문 줄내용
1-25필수 생명주기
26-68Driver 전용 container 예제
69-99기본 API
100-116독립 file handle helper
117-132유일한 open 판별
133-136`v4l2-fh.h` kernel-doc

2. 영어 원문 전체

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

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 V4L2 File handles
4 -----------------
5
6 struct v4l2_fh provides a way to easily keep file handle specific data that is
7 used by the V4L2 framework. Its usage is mandatory in all drivers.
8
9 struct v4l2_fh is allocated in the driver's ``open()`` file operation handler.
10 It is typically embedded in a larger driver-specific structure. The
11 :c:type:`v4l2_fh` must be initialized with a call to :c:func:`v4l2_fh_init`,
12 and added to the video device with :c:func:`v4l2_fh_add`. This associates the
13 :c:type:`v4l2_fh` with the :c:type:`file` by setting ``file->private_data`` to
14 point to the :c:type:`v4l2_fh`.
15
16 Similarly, the struct v4l2_fh is freed in the driver's ``release()`` file
17 operation handler. It must be removed from the video device with
18 :c:func:`v4l2_fh_del` and cleaned up with :c:func:`v4l2_fh_exit` before being
19 freed.
20
21 Drivers must not access ``file->private_data`` directly. They can retrieve the
22 :c:type:`v4l2_fh` associated with a :c:type:`file` by calling
23 :c:func:`file_to_v4l2_fh`. Drivers can extract their own file handle structure
24 by using the container_of macro.
25
26 Example:
27
28 .. code-block:: c
29
30 struct my_fh {
31 int blah;
32 struct v4l2_fh fh;
33 };
34
35 ...
36
37 int my_open(struct file *file)
38 {
39 struct my_fh *my_fh;
40 struct video_device *vfd;
41 int ret;
42
43 ...
44
45 my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
46
47 ...
48
49 v4l2_fh_init(&my_fh->fh, vfd);
50
51 ...
52
53 v4l2_fh_add(&my_fh->fh, file);
54 return 0;
55 }
56
57 int my_release(struct file *file)
58 {
59 struct v4l2_fh *fh = file_to_v4l2_fh(file);
60 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
61
62 ...
63 v4l2_fh_del(&my_fh->fh, file);
64 v4l2_fh_exit(&my_fh->fh);
65 kfree(my_fh);
66 return 0;
67 }
68
69 Below is a short description of the :c:type:`v4l2_fh` functions used:
70
71 :c:func:`v4l2_fh_init <v4l2_fh_init>`
72 (:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)
73
74 - Initialise the file handle. This **MUST** be performed in the driver's
75 :c:type:`v4l2_file_operations`->open() handler.
76
77 :c:func:`v4l2_fh_add <v4l2_fh_add>`
78 (:c:type:`fh <v4l2_fh>`, struct file \*filp)
79
80 - Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
81 Must be called once the file handle is completely initialized.
82
83 :c:func:`v4l2_fh_del <v4l2_fh_del>`
84 (:c:type:`fh <v4l2_fh>`, struct file \*filp)
85
86 - Unassociate the file handle from :c:type:`video_device`. The file handle
87 exit function may now be called.
88
89 :c:func:`v4l2_fh_exit <v4l2_fh_exit>`
90 (:c:type:`fh <v4l2_fh>`)
91
92 - Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
93 memory can be freed.
94
95 :c:func:`file_to_v4l2_fh <file_to_v4l2_fh>`
96 (struct file \*filp)
97
98 - Retrieve the :c:type:`v4l2_fh` instance associated with a :c:type:`file`.
99
100 If struct v4l2_fh is not embedded, then you can use these helper functions:
101
102 :c:func:`v4l2_fh_open <v4l2_fh_open>`
103 (struct file \*filp)
104
105 - This allocates a struct v4l2_fh, initializes it and adds it to
106 the struct video_device associated with the file struct.
107
108 :c:func:`v4l2_fh_release <v4l2_fh_release>`
109 (struct file \*filp)
110
111 - This deletes it from the struct video_device associated with the
112 file struct, uninitialised the :c:type:`v4l2_fh` and frees it.
113
114 These two functions can be plugged into the v4l2_file_operation's ``open()``
115 and ``release()`` ops.
116
117 Several drivers need to do something when the first file handle is opened and
118 when the last file handle closes. Two helper functions were added to check
119 whether the :c:type:`v4l2_fh` struct is the only open filehandle of the
120 associated device node:
121
122 :c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
123 (:c:type:`fh <v4l2_fh>`)
124
125 - Returns 1 if the file handle is the only open file handle, else 0.
126
127 :c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
128 (struct file \*filp)
129
130 - Same, but it calls v4l2_fh_is_singular with filp->private_data.
131
132
133 V4L2 fh functions and data structures
134 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135
136 .. kernel-doc:: include/media/v4l2-fh.h
137

3. 한국어 전문 번역

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

v4l2_fh 생명주기

1-25

`struct v4l2_fh`는 V4L2 framework가 사용하는 file handle별 자료를 쉽게 보관하는 방법이며 모든 driver에서 반드시 사용해야 합니다.

Driver의 `open()` file operation에서 `v4l2_fh`를 할당합니다. 보통 더 큰 driver 전용 file handle 구조체 안에 포함합니다.

`v4l2_fh_init()`으로 초기화한 뒤 `v4l2_fh_add()`로 video device에 추가합니다. 이 과정에서 `file->private_data`가 `v4l2_fh`를 가리키도록 설정되어 file과 연결됩니다.

Driver의 `release()`에서는 먼저 `v4l2_fh_del()`로 video device에서 제거하고 `v4l2_fh_exit()`으로 정리한 뒤 메모리를 해제해야 합니다.

Driver는 `file->private_data`에 직접 접근해서는 안 됩니다. `file_to_v4l2_fh()`로 file에 연결된 `v4l2_fh`를 얻고, `container_of`로 driver 전용 file handle 구조체를 얻을 수 있습니다.

v4l2_fh 생명주기
`open()`할당`v4l2_fh_init()``v4l2_fh_add()`
사용`file_to_v4l2_fh()``container_of`
`release()``v4l2_fh_del()``v4l2_fh_exit()`해제

Open과 release의 초기화·등록·제거·정리 순서를 대칭으로 유지합니다.

.. SPDX-License-Identifier: GPL-2.0

V4L2 File handles
-----------------

struct v4l2_fh provides a way to easily keep file handle specific data that is
used by the V4L2 framework. Its usage is mandatory in all drivers.

struct v4l2_fh is allocated in the driver's ``open()`` file operation handler.
It is typically embedded in a larger driver-specific structure. The
:c:type:`v4l2_fh` must be initialized with a call to :c:func:`v4l2_fh_init`,
and added to the video device with :c:func:`v4l2_fh_add`. This associates the
:c:type:`v4l2_fh` with the :c:type:`file` by setting ``file->private_data`` to
point to the :c:type:`v4l2_fh`.

Similarly, the struct v4l2_fh is freed in the driver's ``release()`` file
operation handler. It must be removed from the video device with
:c:func:`v4l2_fh_del` and cleaned up with :c:func:`v4l2_fh_exit` before being
freed.

Drivers must not access ``file->private_data`` directly. They can retrieve the
:c:type:`v4l2_fh` associated with a :c:type:`file` by calling
:c:func:`file_to_v4l2_fh`. Drivers can extract their own file handle structure
by using the container_of macro.

Driver 전용 file handle 예제

26-68

예제의 `struct my_fh`는 driver 전용 `blah` 자료와 `struct v4l2_fh fh`를 함께 포함합니다.

`my_open()`은 `my_fh`를 `kzalloc()`으로 할당하고 `v4l2_fh_init(&my_fh->fh, vfd)`으로 초기화한 뒤, 모든 file handle 초기화가 끝나면 `v4l2_fh_add(&my_fh->fh, file)`를 호출합니다.

`my_release()`는 `file_to_v4l2_fh(file)`로 framework file handle을 찾고 `container_of()`로 `my_fh`를 복원합니다. 이어서 `v4l2_fh_del()`, `v4l2_fh_exit()`, `kfree()` 순서로 제거·정리·해제합니다.

예제 open과 release
`my_open()``kzalloc()``v4l2_fh_init()``v4l2_fh_add()`
`my_release()``file_to_v4l2_fh()``container_of()``v4l2_fh_del()``v4l2_fh_exit()``kfree()`

Driver 전용 container와 framework file handle 사이의 변환을 보여 줍니다.

Example:

.. code-block:: c

        struct my_fh {
                int blah;
                struct v4l2_fh fh;
        };

        ...

        int my_open(struct file *file)
        {
                struct my_fh *my_fh;
                struct video_device *vfd;
                int ret;

                ...

                my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);

                ...

                v4l2_fh_init(&my_fh->fh, vfd);

                ...

                v4l2_fh_add(&my_fh->fh, file);
                return 0;
        }

        int my_release(struct file *file)
        {
                struct v4l2_fh *fh = file_to_v4l2_fh(file);
                struct my_fh *my_fh = container_of(fh, struct my_fh, fh);

                ...
                v4l2_fh_del(&my_fh->fh, file);
                v4l2_fh_exit(&my_fh->fh);
                kfree(my_fh);
                return 0;
        }

기본 v4l2_fh 함수

69-99

`v4l2_fh_init(fh, vdev)`은 file handle을 초기화하며 driver의 `v4l2_file_operations->open()`에서 반드시 수행해야 합니다.

`v4l2_fh_add(fh, filp)`는 완전히 초기화된 `v4l2_fh`를 `video_device`의 file handle 목록에 추가합니다.

`v4l2_fh_del(fh, filp)`은 file handle과 `video_device`의 연결을 끊습니다. 이 호출 뒤에 file handle exit를 실행할 수 있습니다.

`v4l2_fh_exit(fh)`은 file handle 초기화를 해제합니다. 이후 `v4l2_fh` 메모리를 해제할 수 있습니다.

`file_to_v4l2_fh(filp)`는 `file`에 연결된 `v4l2_fh` instance를 반환합니다.

기본 file handle API
함수역할
`v4l2_fh_init()`File handle 초기화
`v4l2_fh_add()`Video device 목록에 추가
`v4l2_fh_del()`Video device와 연결 해제
`v4l2_fh_exit()`File handle 정리
`file_to_v4l2_fh()`File에서 `v4l2_fh` 검색

Below is a short description of the :c:type:`v4l2_fh` functions used:

:c:func:`v4l2_fh_init <v4l2_fh_init>`
(:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)

- Initialise the file handle. This **MUST** be performed in the driver's
  :c:type:`v4l2_file_operations`->open() handler.

:c:func:`v4l2_fh_add <v4l2_fh_add>`
(:c:type:`fh <v4l2_fh>`, struct file \*filp)

- Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
  Must be called once the file handle is completely initialized.

:c:func:`v4l2_fh_del <v4l2_fh_del>`
(:c:type:`fh <v4l2_fh>`, struct file \*filp)

- Unassociate the file handle from :c:type:`video_device`. The file handle
  exit function may now be called.

:c:func:`v4l2_fh_exit <v4l2_fh_exit>`
(:c:type:`fh <v4l2_fh>`)

- Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
  memory can be freed.

:c:func:`file_to_v4l2_fh <file_to_v4l2_fh>`
(struct file \*filp)

- Retrieve the :c:type:`v4l2_fh` instance associated with a :c:type:`file`.

독립 v4l2_fh helper

100-116

`v4l2_fh`를 driver 전용 구조체에 포함하지 않는 경우 `v4l2_fh_open()`과 `v4l2_fh_release()` helper를 사용할 수 있습니다.

`v4l2_fh_open(filp)`은 `v4l2_fh`를 할당·초기화하고 file에 연결된 `video_device`에 추가합니다.

`v4l2_fh_release(filp)`는 해당 `video_device`에서 file handle을 제거하고 초기화를 해제한 뒤 메모리도 해제합니다.

두 함수는 `v4l2_file_operations`의 `open()`과 `release()` operation에 직접 연결할 수 있습니다.

독립 file handle helper
`open()``v4l2_fh_open()`할당·초기화·추가
`release()``v4l2_fh_release()`제거·정리·해제

포함 구조체가 없을 때 framework가 전체 수명주기를 대신 수행합니다.

If struct v4l2_fh is not embedded, then you can use these helper functions:

:c:func:`v4l2_fh_open <v4l2_fh_open>`
(struct file \*filp)

- This allocates a struct v4l2_fh, initializes it and adds it to
  the struct video_device associated with the file struct.

:c:func:`v4l2_fh_release <v4l2_fh_release>`
(struct file \*filp)

- This deletes it from the struct video_device associated with the
  file struct, uninitialised the :c:type:`v4l2_fh` and frees it.

These two functions can be plugged into the v4l2_file_operation's ``open()``
and ``release()`` ops.

첫 open과 마지막 close 판별

117-132

여러 driver는 첫 file handle이 열릴 때와 마지막 file handle이 닫힐 때 별도 작업이 필요합니다. 이를 위해 해당 `v4l2_fh`가 연관 device node의 유일한 open file handle인지 검사하는 helper가 제공됩니다.

`v4l2_fh_is_singular(fh)`은 유일한 open file handle이면 1, 아니면 0을 반환합니다.

`v4l2_fh_is_singular_file(filp)`은 같은 검사이지만 `filp->private_data`를 사용해 `v4l2_fh_is_singular()`을 호출합니다.

Singular file handle 검사
함수입력반환
`v4l2_fh_is_singular()``v4l2_fh`유일하면 1
`v4l2_fh_is_singular_file()``file`유일하면 1

Several drivers need to do something when the first file handle is opened and
when the last file handle closes. Two helper functions were added to check
whether the :c:type:`v4l2_fh` struct is the only open filehandle of the
associated device node:

:c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
(:c:type:`fh <v4l2_fh>`)

-  Returns 1 if the file handle is the only open file handle, else 0.

:c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
(struct file \*filp)

- Same, but it calls v4l2_fh_is_singular with filp->private_data.

V4L2 fh 함수와 자료구조

133-136

`include/media/v4l2-fh.h`의 kernel-doc에서 V4L2 file handle 함수와 자료구조의 상세 API를 제공합니다.

API 정의 위치
Header내용
`include/media/v4l2-fh.h`V4L2 file handle 함수·자료구조

V4L2 fh functions and data structures
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. kernel-doc:: include/media/v4l2-fh.h