Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 1 | V4L2 File handlers |
| 2 | ------------------ |
| 3 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 4 | struct :c:type:`v4l2_fh` provides a way to easily keep file handle specific |
| 5 | data that is used by the V4L2 framework. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 6 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 7 | .. 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 Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 11 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 12 | The users of :c:type:`v4l2_fh` (in the V4L2 framework, not the driver) know |
| 13 | whether a driver uses :c:type:`v4l2_fh` as its ``file->private_data`` pointer |
| 14 | by testing the ``V4L2_FL_USES_V4L2_FH`` bit in :c:type:`video_device`->flags. |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 15 | This bit is set whenever :c:func:`v4l2_fh_init` is called. |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 16 | |
| 17 | struct :c:type:`v4l2_fh` is allocated as a part of the driver's own file handle |
| 18 | structure and ``file->private_data`` is set to it in the driver's ``open()`` |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 19 | function by the driver. |
| 20 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 21 | In many cases the struct :c:type:`v4l2_fh` will be embedded in a larger |
| 22 | structure. In that case you should call: |
| 23 | |
Mauro Carvalho Chehab | 1b81f01 | 2016-08-19 12:00:43 -0300 | [diff] [blame] | 24 | #) :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 Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 26 | |
| 27 | Drivers can extract their own file handle structure by using the container_of |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 28 | macro. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 29 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 30 | Example: |
| 31 | |
| 32 | .. code-block:: c |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 33 | |
| 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 Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 74 | Below is a short description of the :c:type:`v4l2_fh` functions used: |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 75 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 76 | :c:func:`v4l2_fh_init <v4l2_fh_init>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 77 | (:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 78 | |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 79 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 80 | - Initialise the file handle. This **MUST** be performed in the driver's |
| 81 | :c:type:`v4l2_file_operations`->open() handler. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 82 | |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 83 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 84 | :c:func:`v4l2_fh_add <v4l2_fh_add>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 85 | (:c:type:`fh <v4l2_fh>`) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 86 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 87 | - 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 Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 89 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 90 | :c:func:`v4l2_fh_del <v4l2_fh_del>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 91 | (:c:type:`fh <v4l2_fh>`) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 92 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 93 | - Unassociate the file handle from :c:type:`video_device`. The file handle |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 94 | exit function may now be called. |
| 95 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 96 | :c:func:`v4l2_fh_exit <v4l2_fh_exit>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 97 | (:c:type:`fh <v4l2_fh>`) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 98 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 99 | - Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh` |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 100 | memory can be freed. |
| 101 | |
| 102 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 103 | If struct :c:type:`v4l2_fh` is not embedded, then you can use these helper functions: |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 104 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 105 | :c:func:`v4l2_fh_open <v4l2_fh_open>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 106 | (struct file \*filp) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 107 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 108 | - 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 Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 111 | :c:func:`v4l2_fh_release <v4l2_fh_release>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 112 | (struct file \*filp) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 113 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 114 | - 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 Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 116 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 117 | These two functions can be plugged into the v4l2_file_operation's ``open()`` |
| 118 | and ``release()`` ops. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 119 | |
| 120 | Several drivers need to do something when the first file handle is opened and |
| 121 | when the last file handle closes. Two helper functions were added to check |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 122 | whether the :c:type:`v4l2_fh` struct is the only open filehandle of the |
| 123 | associated device node: |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 124 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 125 | :c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 126 | (:c:type:`fh <v4l2_fh>`) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 127 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 128 | - Returns 1 if the file handle is the only open file handle, else 0. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 129 | |
Mauro Carvalho Chehab | 7b998ba | 2016-07-23 07:21:06 -0300 | [diff] [blame] | 130 | :c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>` |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 131 | (struct file \*filp) |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 132 | |
Mauro Carvalho Chehab | 46f74f1 | 2016-07-22 09:31:16 -0300 | [diff] [blame] | 133 | - Same, but it calls v4l2_fh_is_singular with filp->private_data. |
Mauro Carvalho Chehab | 378d4a5 | 2016-07-22 09:10:20 -0300 | [diff] [blame] | 134 | |
| 135 | |
Mauro Carvalho Chehab | f6fa883 | 2016-07-22 10:15:42 -0300 | [diff] [blame] | 136 | V4L2 fh functions and data structures |
| 137 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 138 | |
Mauro Carvalho Chehab | 4ada120 | 2016-07-22 09:06:45 -0300 | [diff] [blame] | 139 | .. kernel-doc:: include/media/v4l2-fh.h |