v4l2-event.rst 5.3 KB

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