Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 V4L2 events
0004 -----------
0005 
0006 The V4L2 events provide a generic way to pass events to user space.
0007 The driver must use :c:type:`v4l2_fh` to be able to support V4L2 events.
0008 
0009 Events are subscribed per-filehandle. An event specification consists of a
0010 ``type`` and is optionally associated with an object identified through the
0011 ``id`` field. If unused, then the ``id`` is 0. So an event is uniquely
0012 identified by the ``(type, id)`` tuple.
0013 
0014 The :c:type:`v4l2_fh` struct has a list of subscribed events on its
0015 ``subscribed`` field.
0016 
0017 When the user subscribes to an event, a :c:type:`v4l2_subscribed_event`
0018 struct is added to :c:type:`v4l2_fh`\ ``.subscribed``, one for every
0019 subscribed event.
0020 
0021 Each :c:type:`v4l2_subscribed_event` struct ends with a
0022 :c:type:`v4l2_kevent` ringbuffer, with the size given by the caller
0023 of :c:func:`v4l2_event_subscribe`. This ringbuffer is used to store any events
0024 raised by the driver.
0025 
0026 So every ``(type, ID)`` event tuple will have its own
0027 :c:type:`v4l2_kevent` ringbuffer. This guarantees that if a driver is
0028 generating lots of events of one type in a short time, then that will
0029 not overwrite events of another type.
0030 
0031 But if you get more events of one type than the size of the
0032 :c:type:`v4l2_kevent` ringbuffer, then the oldest event will be dropped
0033 and the new one added.
0034 
0035 The :c:type:`v4l2_kevent` struct links into the ``available``
0036 list of the :c:type:`v4l2_fh` struct so :ref:`VIDIOC_DQEVENT` will
0037 know which event to dequeue first.
0038 
0039 Finally, if the event subscription is associated with a particular object
0040 such as a V4L2 control, then that object needs to know about that as well
0041 so that an event can be raised by that object. So the ``node`` field can
0042 be used to link the :c:type:`v4l2_subscribed_event` struct into a list of
0043 such objects.
0044 
0045 So to summarize:
0046 
0047 - struct v4l2_fh has two lists: one of the ``subscribed`` events,
0048   and one of the ``available`` events.
0049 
0050 - struct v4l2_subscribed_event has a ringbuffer of raised
0051   (pending) events of that particular type.
0052 
0053 - If struct v4l2_subscribed_event is associated with a specific
0054   object, then that object will have an internal list of
0055   struct v4l2_subscribed_event so it knows who subscribed an
0056   event to that object.
0057 
0058 Furthermore, the internal struct v4l2_subscribed_event has
0059 ``merge()`` and ``replace()`` callbacks which drivers can set. These
0060 callbacks are called when a new event is raised and there is no more room.
0061 
0062 The ``replace()`` callback allows you to replace the payload of the old event
0063 with that of the new event, merging any relevant data from the old payload
0064 into the new payload that replaces it. It is called when this event type has
0065 a ringbuffer with size is one, i.e. only one event can be stored in the
0066 ringbuffer.
0067 
0068 The ``merge()`` callback allows you to merge the oldest event payload into
0069 that of the second-oldest event payload. It is called when
0070 the ringbuffer has size is greater than one.
0071 
0072 This way no status information is lost, just the intermediate steps leading
0073 up to that state.
0074 
0075 A good example of these ``replace``/``merge`` callbacks is in v4l2-event.c:
0076 ``ctrls_replace()`` and ``ctrls_merge()`` callbacks for the control event.
0077 
0078 .. note::
0079         these callbacks can be called from interrupt context, so they must
0080         be fast.
0081 
0082 In order to queue events to video device, drivers should call:
0083 
0084         :c:func:`v4l2_event_queue <v4l2_event_queue>`
0085         (:c:type:`vdev <video_device>`, :c:type:`ev <v4l2_event>`)
0086 
0087 The driver's only responsibility is to fill in the type and the data fields.
0088 The other fields will be filled in by V4L2.
0089 
0090 Event subscription
0091 ~~~~~~~~~~~~~~~~~~
0092 
0093 Subscribing to an event is via:
0094 
0095         :c:func:`v4l2_event_subscribe <v4l2_event_subscribe>`
0096         (:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>` ,
0097         elems, :c:type:`ops <v4l2_subscribed_event_ops>`)
0098 
0099 
0100 This function is used to implement :c:type:`video_device`->
0101 :c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_subscribe_event``,
0102 but the driver must check first if the driver is able to produce events
0103 with specified event id, and then should call
0104 :c:func:`v4l2_event_subscribe` to subscribe the event.
0105 
0106 The elems argument is the size of the event queue for this event. If it is 0,
0107 then the framework will fill in a default value (this depends on the event
0108 type).
0109 
0110 The ops argument allows the driver to specify a number of callbacks:
0111 
0112 .. tabularcolumns:: |p{1.5cm}|p{16.0cm}|
0113 
0114 ======== ==============================================================
0115 Callback Description
0116 ======== ==============================================================
0117 add      called when a new listener gets added (subscribing to the same
0118          event twice will only cause this callback to get called once)
0119 del      called when a listener stops listening
0120 replace  replace event 'old' with event 'new'.
0121 merge    merge event 'old' into event 'new'.
0122 ======== ==============================================================
0123 
0124 All 4 callbacks are optional, if you don't want to specify any callbacks
0125 the ops argument itself maybe ``NULL``.
0126 
0127 Unsubscribing an event
0128 ~~~~~~~~~~~~~~~~~~~~~~
0129 
0130 Unsubscribing to an event is via:
0131 
0132         :c:func:`v4l2_event_unsubscribe <v4l2_event_unsubscribe>`
0133         (:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>`)
0134 
0135 This function is used to implement :c:type:`video_device`->
0136 :c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_unsubscribe_event``.
0137 A driver may call :c:func:`v4l2_event_unsubscribe` directly unless it
0138 wants to be involved in unsubscription process.
0139 
0140 The special type ``V4L2_EVENT_ALL`` may be used to unsubscribe all events. The
0141 drivers may want to handle this in a special way.
0142 
0143 Check if there's a pending event
0144 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0145 
0146 Checking if there's a pending event is via:
0147 
0148         :c:func:`v4l2_event_pending <v4l2_event_pending>`
0149         (:c:type:`fh <v4l2_fh>`)
0150 
0151 
0152 This function returns the number of pending events. Useful when implementing
0153 poll.
0154 
0155 How events work
0156 ~~~~~~~~~~~~~~~
0157 
0158 Events are delivered to user space through the poll system call. The driver
0159 can use :c:type:`v4l2_fh`->wait (a wait_queue_head_t) as the argument for
0160 ``poll_wait()``.
0161 
0162 There are standard and private events. New standard events must use the
0163 smallest available event type. The drivers must allocate their events from
0164 their own class starting from class base. Class base is
0165 ``V4L2_EVENT_PRIVATE_START`` + n * 1000 where n is the lowest available number.
0166 The first event type in the class is reserved for future use, so the first
0167 available event type is 'class base + 1'.
0168 
0169 An example on how the V4L2 events may be used can be found in the OMAP
0170 3 ISP driver (``drivers/media/platform/ti/omap3isp``).
0171 
0172 A subdev can directly send an event to the :c:type:`v4l2_device` notify
0173 function with ``V4L2_DEVICE_NOTIFY_EVENT``. This allows the bridge to map
0174 the subdev that sends the event to the video node(s) associated with the
0175 subdev that need to be informed about such an event.
0176 
0177 V4L2 event functions and data structures
0178 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0179 
0180 .. kernel-doc:: include/media/v4l2-event.h
0181