1 .. SPDX-License-Identifier: GPL-2.0
6 struct :c:type:`v4l2_fh` provides a way to easily keep file handle specific
7 data that is used by the V4L2 framework.
10 New drivers must use struct :c:type:`v4l2_fh`
11 since it is also used to implement priority handling
12 (:ref:`VIDIOC_G_PRIORITY`).
14 The users of :c:type:`v4l2_fh` (in the V4L2 framework, not the driver) know
15 whether a driver uses :c:type:`v4l2_fh` as its ``file->private_data`` pointer
16 by testing the ``V4L2_FL_USES_V4L2_FH`` bit in :c:type:`video_device`->flags.
17 This bit is set whenever :c:func:`v4l2_fh_init` is called.
19 struct :c:type:`v4l2_fh` is allocated as a part of the driver's own file handle
20 structure and ``file->private_data`` is set to it in the driver's ``open()``
21 function by the driver.
23 In many cases the struct :c:type:`v4l2_fh` will be embedded in a larger
24 structure. In that case you should call:
26 #) :c:func:`v4l2_fh_init` and :c:func:`v4l2_fh_add` in ``open()``
27 #) :c:func:`v4l2_fh_del` and :c:func:`v4l2_fh_exit` in ``release()``
29 Drivers can extract their own file handle structure by using the container_of
43 int my_open(struct file *file)
46 struct video_device *vfd;
51 my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
55 v4l2_fh_init(&my_fh->fh, vfd);
59 file->private_data = &my_fh->fh;
60 v4l2_fh_add(&my_fh->fh);
64 int my_release(struct file *file)
66 struct v4l2_fh *fh = file->private_data;
67 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
70 v4l2_fh_del(&my_fh->fh);
71 v4l2_fh_exit(&my_fh->fh);
76 Below is a short description of the :c:type:`v4l2_fh` functions used:
78 :c:func:`v4l2_fh_init <v4l2_fh_init>`
79 (:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)
82 - Initialise the file handle. This **MUST** be performed in the driver's
83 :c:type:`v4l2_file_operations`->open() handler.
86 :c:func:`v4l2_fh_add <v4l2_fh_add>`
87 (:c:type:`fh <v4l2_fh>`)
89 - Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
90 Must be called once the file handle is completely initialized.
92 :c:func:`v4l2_fh_del <v4l2_fh_del>`
93 (:c:type:`fh <v4l2_fh>`)
95 - Unassociate the file handle from :c:type:`video_device`. The file handle
96 exit function may now be called.
98 :c:func:`v4l2_fh_exit <v4l2_fh_exit>`
99 (:c:type:`fh <v4l2_fh>`)
101 - Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
105 If struct :c:type:`v4l2_fh` is not embedded, then you can use these helper functions:
107 :c:func:`v4l2_fh_open <v4l2_fh_open>`
110 - This allocates a struct :c:type:`v4l2_fh`, initializes it and adds it to
111 the struct :c:type:`video_device` associated with the file struct.
113 :c:func:`v4l2_fh_release <v4l2_fh_release>`
116 - This deletes it from the struct :c:type:`video_device` associated with the
117 file struct, uninitialised the :c:type:`v4l2_fh` and frees it.
119 These two functions can be plugged into the v4l2_file_operation's ``open()``
120 and ``release()`` ops.
122 Several drivers need to do something when the first file handle is opened and
123 when the last file handle closes. Two helper functions were added to check
124 whether the :c:type:`v4l2_fh` struct is the only open filehandle of the
125 associated device node:
127 :c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
128 (:c:type:`fh <v4l2_fh>`)
130 - Returns 1 if the file handle is the only open file handle, else 0.
132 :c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
135 - Same, but it calls v4l2_fh_is_singular with filp->private_data.
138 V4L2 fh functions and data structures
139 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141 .. kernel-doc:: include/media/v4l2-fh.h