v4l2-fh.rst 3.7 KB

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