xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/media/v4l2-fh.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunV4L2 File handlers
4*4882a593Smuzhiyun------------------
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunstruct v4l2_fh provides a way to easily keep file handle specific
7*4882a593Smuzhiyundata that is used by the V4L2 framework.
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun.. attention::
10*4882a593Smuzhiyun	New drivers must use struct v4l2_fh
11*4882a593Smuzhiyun	since it is also used to implement priority handling
12*4882a593Smuzhiyun	(:ref:`VIDIOC_G_PRIORITY`).
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe users of :c:type:`v4l2_fh` (in the V4L2 framework, not the driver) know
15*4882a593Smuzhiyunwhether a driver uses :c:type:`v4l2_fh` as its ``file->private_data`` pointer
16*4882a593Smuzhiyunby testing the ``V4L2_FL_USES_V4L2_FH`` bit in :c:type:`video_device`->flags.
17*4882a593SmuzhiyunThis bit is set whenever :c:func:`v4l2_fh_init` is called.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyunstruct v4l2_fh is allocated as a part of the driver's own file handle
20*4882a593Smuzhiyunstructure and ``file->private_data`` is set to it in the driver's ``open()``
21*4882a593Smuzhiyunfunction by the driver.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunIn many cases the struct v4l2_fh will be embedded in a larger
24*4882a593Smuzhiyunstructure. In that case you should call:
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun#) :c:func:`v4l2_fh_init` and :c:func:`v4l2_fh_add` in ``open()``
27*4882a593Smuzhiyun#) :c:func:`v4l2_fh_del` and :c:func:`v4l2_fh_exit` in ``release()``
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunDrivers can extract their own file handle structure by using the container_of
30*4882a593Smuzhiyunmacro.
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunExample:
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun.. code-block:: c
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun	struct my_fh {
37*4882a593Smuzhiyun		int blah;
38*4882a593Smuzhiyun		struct v4l2_fh fh;
39*4882a593Smuzhiyun	};
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun	...
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun	int my_open(struct file *file)
44*4882a593Smuzhiyun	{
45*4882a593Smuzhiyun		struct my_fh *my_fh;
46*4882a593Smuzhiyun		struct video_device *vfd;
47*4882a593Smuzhiyun		int ret;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun		...
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun		my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun		...
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun		v4l2_fh_init(&my_fh->fh, vfd);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun		...
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun		file->private_data = &my_fh->fh;
60*4882a593Smuzhiyun		v4l2_fh_add(&my_fh->fh);
61*4882a593Smuzhiyun		return 0;
62*4882a593Smuzhiyun	}
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun	int my_release(struct file *file)
65*4882a593Smuzhiyun	{
66*4882a593Smuzhiyun		struct v4l2_fh *fh = file->private_data;
67*4882a593Smuzhiyun		struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun		...
70*4882a593Smuzhiyun		v4l2_fh_del(&my_fh->fh);
71*4882a593Smuzhiyun		v4l2_fh_exit(&my_fh->fh);
72*4882a593Smuzhiyun		kfree(my_fh);
73*4882a593Smuzhiyun		return 0;
74*4882a593Smuzhiyun	}
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunBelow is a short description of the :c:type:`v4l2_fh` functions used:
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun:c:func:`v4l2_fh_init <v4l2_fh_init>`
79*4882a593Smuzhiyun(:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun- Initialise the file handle. This **MUST** be performed in the driver's
83*4882a593Smuzhiyun  :c:type:`v4l2_file_operations`->open() handler.
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun:c:func:`v4l2_fh_add <v4l2_fh_add>`
87*4882a593Smuzhiyun(:c:type:`fh <v4l2_fh>`)
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun- Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
90*4882a593Smuzhiyun  Must be called once the file handle is completely initialized.
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun:c:func:`v4l2_fh_del <v4l2_fh_del>`
93*4882a593Smuzhiyun(:c:type:`fh <v4l2_fh>`)
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun- Unassociate the file handle from :c:type:`video_device`. The file handle
96*4882a593Smuzhiyun  exit function may now be called.
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun:c:func:`v4l2_fh_exit <v4l2_fh_exit>`
99*4882a593Smuzhiyun(:c:type:`fh <v4l2_fh>`)
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun- Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
102*4882a593Smuzhiyun  memory can be freed.
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunIf struct v4l2_fh is not embedded, then you can use these helper functions:
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun:c:func:`v4l2_fh_open <v4l2_fh_open>`
108*4882a593Smuzhiyun(struct file \*filp)
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun- This allocates a struct v4l2_fh, initializes it and adds it to
111*4882a593Smuzhiyun  the struct video_device associated with the file struct.
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun:c:func:`v4l2_fh_release <v4l2_fh_release>`
114*4882a593Smuzhiyun(struct file \*filp)
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun- This deletes it from the struct video_device associated with the
117*4882a593Smuzhiyun  file struct, uninitialised the :c:type:`v4l2_fh` and frees it.
118*4882a593Smuzhiyun
119*4882a593SmuzhiyunThese two functions can be plugged into the v4l2_file_operation's ``open()``
120*4882a593Smuzhiyunand ``release()`` ops.
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunSeveral drivers need to do something when the first file handle is opened and
123*4882a593Smuzhiyunwhen the last file handle closes. Two helper functions were added to check
124*4882a593Smuzhiyunwhether the :c:type:`v4l2_fh` struct is the only open filehandle of the
125*4882a593Smuzhiyunassociated device node:
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun:c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
128*4882a593Smuzhiyun(:c:type:`fh <v4l2_fh>`)
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun-  Returns 1 if the file handle is the only open file handle, else 0.
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun:c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
133*4882a593Smuzhiyun(struct file \*filp)
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun- Same, but it calls v4l2_fh_is_singular with filp->private_data.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunV4L2 fh functions and data structures
139*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun.. kernel-doc:: include/media/v4l2-fh.h
142