blob: 3ee64adf46358128bec5f71c9c3ed5c1aebe80d7 [file] [log] [blame]
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -03001V4L2 File handlers
2------------------
3
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -03004struct :c:type:`v4l2_fh` provides a way to easily keep file handle specific
5data that is used by the V4L2 framework.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -03006
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -03007.. attention::
8 New drivers must use struct :c:type:`v4l2_fh`
9 since it is also used to implement priority handling
10 (:ref:`VIDIOC_G_PRIORITY`).
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030011
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030012The users of :c:type:`v4l2_fh` (in the V4L2 framework, not the driver) know
13whether a driver uses :c:type:`v4l2_fh` as its ``file->private_data`` pointer
14by testing the ``V4L2_FL_USES_V4L2_FH`` bit in :c:type:`video_device`->flags.
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -030015This bit is set whenever :c:func:`v4l2_fh_init` is called.
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030016
17struct :c:type:`v4l2_fh` is allocated as a part of the driver's own file handle
18structure and ``file->private_data`` is set to it in the driver's ``open()``
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030019function by the driver.
20
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030021In many cases the struct :c:type:`v4l2_fh` will be embedded in a larger
22structure. In that case you should call:
23
Mauro Carvalho Chehab1b81f012016-08-19 12:00:43 -030024#) :c:func:`v4l2_fh_init` and :c:func:`v4l2_fh_add` in ``open()``
25#) :c:func:`v4l2_fh_del` and :c:func:`v4l2_fh_exit` in ``release()``
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030026
27Drivers can extract their own file handle structure by using the container_of
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030028macro.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030029
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030030Example:
31
32.. code-block:: c
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030033
34 struct my_fh {
35 int blah;
36 struct v4l2_fh fh;
37 };
38
39 ...
40
41 int my_open(struct file *file)
42 {
43 struct my_fh *my_fh;
44 struct video_device *vfd;
45 int ret;
46
47 ...
48
49 my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
50
51 ...
52
53 v4l2_fh_init(&my_fh->fh, vfd);
54
55 ...
56
57 file->private_data = &my_fh->fh;
58 v4l2_fh_add(&my_fh->fh);
59 return 0;
60 }
61
62 int my_release(struct file *file)
63 {
64 struct v4l2_fh *fh = file->private_data;
65 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
66
67 ...
68 v4l2_fh_del(&my_fh->fh);
69 v4l2_fh_exit(&my_fh->fh);
70 kfree(my_fh);
71 return 0;
72 }
73
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030074Below is a short description of the :c:type:`v4l2_fh` functions used:
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030075
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -030076:c:func:`v4l2_fh_init <v4l2_fh_init>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030077(:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030078
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030079
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030080- Initialise the file handle. This **MUST** be performed in the driver's
81 :c:type:`v4l2_file_operations`->open() handler.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030082
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030083
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -030084:c:func:`v4l2_fh_add <v4l2_fh_add>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030085(:c:type:`fh <v4l2_fh>`)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030086
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030087- Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
88 Must be called once the file handle is completely initialized.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030089
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -030090:c:func:`v4l2_fh_del <v4l2_fh_del>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030091(:c:type:`fh <v4l2_fh>`)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030092
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030093- Unassociate the file handle from :c:type:`video_device`. The file handle
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030094 exit function may now be called.
95
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -030096:c:func:`v4l2_fh_exit <v4l2_fh_exit>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030097(:c:type:`fh <v4l2_fh>`)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -030098
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -030099- Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300100 memory can be freed.
101
102
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300103If struct :c:type:`v4l2_fh` is not embedded, then you can use these helper functions:
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300104
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -0300105:c:func:`v4l2_fh_open <v4l2_fh_open>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300106(struct file \*filp)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300107
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300108- This allocates a struct :c:type:`v4l2_fh`, initializes it and adds it to
109 the struct :c:type:`video_device` associated with the file struct.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300110
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -0300111:c:func:`v4l2_fh_release <v4l2_fh_release>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300112(struct file \*filp)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300113
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300114- This deletes it from the struct :c:type:`video_device` associated with the
115 file struct, uninitialised the :c:type:`v4l2_fh` and frees it.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300116
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300117These two functions can be plugged into the v4l2_file_operation's ``open()``
118and ``release()`` ops.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300119
120Several drivers need to do something when the first file handle is opened and
121when the last file handle closes. Two helper functions were added to check
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300122whether the :c:type:`v4l2_fh` struct is the only open filehandle of the
123associated device node:
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300124
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -0300125:c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300126(:c:type:`fh <v4l2_fh>`)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300127
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300128- Returns 1 if the file handle is the only open file handle, else 0.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300129
Mauro Carvalho Chehab7b998ba2016-07-23 07:21:06 -0300130:c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300131(struct file \*filp)
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300132
Mauro Carvalho Chehab46f74f12016-07-22 09:31:16 -0300133- Same, but it calls v4l2_fh_is_singular with filp->private_data.
Mauro Carvalho Chehab378d4a52016-07-22 09:10:20 -0300134
135
Mauro Carvalho Chehabf6fa8832016-07-22 10:15:42 -0300136V4L2 fh functions and data structures
137^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138
Mauro Carvalho Chehab4ada1202016-07-22 09:06:45 -0300139.. kernel-doc:: include/media/v4l2-fh.h