v4l2-event.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. .. SPDX-License-Identifier: GPL-2.0
  2. V4L2 events
  3. -----------
  4. The V4L2 events provide a generic way to pass events to user space.
  5. The driver must use :c:type:`v4l2_fh` to be able to support V4L2 events.
  6. Events are subscribed per-filehandle. An event specification consists of a
  7. ``type`` and is optionally associated with an object identified through the
  8. ``id`` field. If unused, then the ``id`` is 0. So an event is uniquely
  9. identified by the ``(type, id)`` tuple.
  10. The :c:type:`v4l2_fh` struct has a list of subscribed events on its
  11. ``subscribed`` field.
  12. When the user subscribes to an event, a :c:type:`v4l2_subscribed_event`
  13. struct is added to :c:type:`v4l2_fh`\ ``.subscribed``, one for every
  14. subscribed event.
  15. Each :c:type:`v4l2_subscribed_event` struct ends with a
  16. :c:type:`v4l2_kevent` ringbuffer, with the size given by the caller
  17. of :c:func:`v4l2_event_subscribe`. This ringbuffer is used to store any events
  18. raised by the driver.
  19. So every ``(type, ID)`` event tuple will have its own
  20. :c:type:`v4l2_kevent` ringbuffer. This guarantees that if a driver is
  21. generating lots of events of one type in a short time, then that will
  22. not overwrite events of another type.
  23. But if you get more events of one type than the size of the
  24. :c:type:`v4l2_kevent` ringbuffer, then the oldest event will be dropped
  25. and the new one added.
  26. The :c:type:`v4l2_kevent` struct links into the ``available``
  27. list of the :c:type:`v4l2_fh` struct so :ref:`VIDIOC_DQEVENT` will
  28. know which event to dequeue first.
  29. Finally, if the event subscription is associated with a particular object
  30. such as a V4L2 control, then that object needs to know about that as well
  31. so that an event can be raised by that object. So the ``node`` field can
  32. be used to link the :c:type:`v4l2_subscribed_event` struct into a list of
  33. such objects.
  34. So to summarize:
  35. - struct v4l2_fh has two lists: one of the ``subscribed`` events,
  36. and one of the ``available`` events.
  37. - struct v4l2_subscribed_event has a ringbuffer of raised
  38. (pending) events of that particular type.
  39. - If struct v4l2_subscribed_event is associated with a specific
  40. object, then that object will have an internal list of
  41. struct v4l2_subscribed_event so it knows who subscribed an
  42. event to that object.
  43. Furthermore, the internal struct v4l2_subscribed_event has
  44. ``merge()`` and ``replace()`` callbacks which drivers can set. These
  45. callbacks are called when a new event is raised and there is no more room.
  46. The ``replace()`` callback allows you to replace the payload of the old event
  47. with that of the new event, merging any relevant data from the old payload
  48. into the new payload that replaces it. It is called when this event type has
  49. a ringbuffer with size is one, i.e. only one event can be stored in the
  50. ringbuffer.
  51. The ``merge()`` callback allows you to merge the oldest event payload into
  52. that of the second-oldest event payload. It is called when
  53. the ringbuffer has size is greater than one.
  54. This way no status information is lost, just the intermediate steps leading
  55. up to that state.
  56. A good example of these ``replace``/``merge`` callbacks is in v4l2-event.c:
  57. ``ctrls_replace()`` and ``ctrls_merge()`` callbacks for the control event.
  58. .. note::
  59. these callbacks can be called from interrupt context, so they must
  60. be fast.
  61. In order to queue events to video device, drivers should call:
  62. :c:func:`v4l2_event_queue <v4l2_event_queue>`
  63. (:c:type:`vdev <video_device>`, :c:type:`ev <v4l2_event>`)
  64. The driver's only responsibility is to fill in the type and the data fields.
  65. The other fields will be filled in by V4L2.
  66. Event subscription
  67. ~~~~~~~~~~~~~~~~~~
  68. Subscribing to an event is via:
  69. :c:func:`v4l2_event_subscribe <v4l2_event_subscribe>`
  70. (:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>` ,
  71. elems, :c:type:`ops <v4l2_subscribed_event_ops>`)
  72. This function is used to implement :c:type:`video_device`->
  73. :c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_subscribe_event``,
  74. but the driver must check first if the driver is able to produce events
  75. with specified event id, and then should call
  76. :c:func:`v4l2_event_subscribe` to subscribe the event.
  77. The elems argument is the size of the event queue for this event. If it is 0,
  78. then the framework will fill in a default value (this depends on the event
  79. type).
  80. The ops argument allows the driver to specify a number of callbacks:
  81. .. tabularcolumns:: |p{1.5cm}|p{16.0cm}|
  82. ======== ==============================================================
  83. Callback Description
  84. ======== ==============================================================
  85. add called when a new listener gets added (subscribing to the same
  86. event twice will only cause this callback to get called once)
  87. del called when a listener stops listening
  88. replace replace event 'old' with event 'new'.
  89. merge merge event 'old' into event 'new'.
  90. ======== ==============================================================
  91. All 4 callbacks are optional, if you don't want to specify any callbacks
  92. the ops argument itself maybe ``NULL``.
  93. Unsubscribing an event
  94. ~~~~~~~~~~~~~~~~~~~~~~
  95. Unsubscribing to an event is via:
  96. :c:func:`v4l2_event_unsubscribe <v4l2_event_unsubscribe>`
  97. (:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>`)
  98. This function is used to implement :c:type:`video_device`->
  99. :c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_unsubscribe_event``.
  100. A driver may call :c:func:`v4l2_event_unsubscribe` directly unless it
  101. wants to be involved in unsubscription process.
  102. The special type ``V4L2_EVENT_ALL`` may be used to unsubscribe all events. The
  103. drivers may want to handle this in a special way.
  104. Check if there's a pending event
  105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. Checking if there's a pending event is via:
  107. :c:func:`v4l2_event_pending <v4l2_event_pending>`
  108. (:c:type:`fh <v4l2_fh>`)
  109. This function returns the number of pending events. Useful when implementing
  110. poll.
  111. How events work
  112. ~~~~~~~~~~~~~~~
  113. Events are delivered to user space through the poll system call. The driver
  114. can use :c:type:`v4l2_fh`->wait (a wait_queue_head_t) as the argument for
  115. ``poll_wait()``.
  116. There are standard and private events. New standard events must use the
  117. smallest available event type. The drivers must allocate their events from
  118. their own class starting from class base. Class base is
  119. ``V4L2_EVENT_PRIVATE_START`` + n * 1000 where n is the lowest available number.
  120. The first event type in the class is reserved for future use, so the first
  121. available event type is 'class base + 1'.
  122. An example on how the V4L2 events may be used can be found in the OMAP
  123. 3 ISP driver (``drivers/media/platform/ti/omap3isp``).
  124. A subdev can directly send an event to the :c:type:`v4l2_device` notify
  125. function with ``V4L2_DEVICE_NOTIFY_EVENT``. This allows the bridge to map
  126. the subdev that sends the event to the video node(s) associated with the
  127. subdev that need to be informed about such an event.
  128. V4L2 event functions and data structures
  129. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  130. .. kernel-doc:: include/media/v4l2-event.h