v4l2-fh.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. .. SPDX-License-Identifier: GPL-2.0
  2. V4L2 File handlers
  3. ------------------
  4. struct v4l2_fh provides a way to easily keep file handle specific
  5. data that is used by the V4L2 framework.
  6. .. attention::
  7. New drivers must use struct v4l2_fh
  8. since it is also used to implement priority handling
  9. (:ref:`VIDIOC_G_PRIORITY`).
  10. The users of :c:type:`v4l2_fh` (in the V4L2 framework, not the driver) know
  11. whether a driver uses :c:type:`v4l2_fh` as its ``file->private_data`` pointer
  12. by testing the ``V4L2_FL_USES_V4L2_FH`` bit in :c:type:`video_device`->flags.
  13. This bit is set whenever :c:func:`v4l2_fh_init` is called.
  14. struct v4l2_fh is allocated as a part of the driver's own file handle
  15. structure and ``file->private_data`` is set to it in the driver's ``open()``
  16. function by the driver.
  17. In many cases the struct v4l2_fh will be embedded in a larger
  18. structure. In that case you should call:
  19. #) :c:func:`v4l2_fh_init` and :c:func:`v4l2_fh_add` in ``open()``
  20. #) :c:func:`v4l2_fh_del` and :c:func:`v4l2_fh_exit` in ``release()``
  21. Drivers can extract their own file handle structure by using the container_of
  22. macro.
  23. Example:
  24. .. code-block:: c
  25. struct my_fh {
  26. int blah;
  27. struct v4l2_fh fh;
  28. };
  29. ...
  30. int my_open(struct file *file)
  31. {
  32. struct my_fh *my_fh;
  33. struct video_device *vfd;
  34. int ret;
  35. ...
  36. my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
  37. ...
  38. v4l2_fh_init(&my_fh->fh, vfd);
  39. ...
  40. file->private_data = &my_fh->fh;
  41. v4l2_fh_add(&my_fh->fh);
  42. return 0;
  43. }
  44. int my_release(struct file *file)
  45. {
  46. struct v4l2_fh *fh = file->private_data;
  47. struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
  48. ...
  49. v4l2_fh_del(&my_fh->fh);
  50. v4l2_fh_exit(&my_fh->fh);
  51. kfree(my_fh);
  52. return 0;
  53. }
  54. Below is a short description of the :c:type:`v4l2_fh` functions used:
  55. :c:func:`v4l2_fh_init <v4l2_fh_init>`
  56. (:c:type:`fh <v4l2_fh>`, :c:type:`vdev <video_device>`)
  57. - Initialise the file handle. This **MUST** be performed in the driver's
  58. :c:type:`v4l2_file_operations`->open() handler.
  59. :c:func:`v4l2_fh_add <v4l2_fh_add>`
  60. (:c:type:`fh <v4l2_fh>`)
  61. - Add a :c:type:`v4l2_fh` to :c:type:`video_device` file handle list.
  62. Must be called once the file handle is completely initialized.
  63. :c:func:`v4l2_fh_del <v4l2_fh_del>`
  64. (:c:type:`fh <v4l2_fh>`)
  65. - Unassociate the file handle from :c:type:`video_device`. The file handle
  66. exit function may now be called.
  67. :c:func:`v4l2_fh_exit <v4l2_fh_exit>`
  68. (:c:type:`fh <v4l2_fh>`)
  69. - Uninitialise the file handle. After uninitialisation the :c:type:`v4l2_fh`
  70. memory can be freed.
  71. If struct v4l2_fh is not embedded, then you can use these helper functions:
  72. :c:func:`v4l2_fh_open <v4l2_fh_open>`
  73. (struct file \*filp)
  74. - This allocates a struct v4l2_fh, initializes it and adds it to
  75. the struct video_device associated with the file struct.
  76. :c:func:`v4l2_fh_release <v4l2_fh_release>`
  77. (struct file \*filp)
  78. - This deletes it from the struct video_device associated with the
  79. file struct, uninitialised the :c:type:`v4l2_fh` and frees it.
  80. These two functions can be plugged into the v4l2_file_operation's ``open()``
  81. and ``release()`` ops.
  82. Several drivers need to do something when the first file handle is opened and
  83. when the last file handle closes. Two helper functions were added to check
  84. whether the :c:type:`v4l2_fh` struct is the only open filehandle of the
  85. associated device node:
  86. :c:func:`v4l2_fh_is_singular <v4l2_fh_is_singular>`
  87. (:c:type:`fh <v4l2_fh>`)
  88. - Returns 1 if the file handle is the only open file handle, else 0.
  89. :c:func:`v4l2_fh_is_singular_file <v4l2_fh_is_singular_file>`
  90. (struct file \*filp)
  91. - Same, but it calls v4l2_fh_is_singular with filp->private_data.
  92. V4L2 fh functions and data structures
  93. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  94. .. kernel-doc:: include/media/v4l2-fh.h