xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/media/v4l2-event.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593SmuzhiyunV4L2 events
4*4882a593Smuzhiyun-----------
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunThe V4L2 events provide a generic way to pass events to user space.
7*4882a593SmuzhiyunThe driver must use :c:type:`v4l2_fh` to be able to support V4L2 events.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunEvents are subscribed per-filehandle. An event specification consists of a
10*4882a593Smuzhiyun``type`` and is optionally associated with an object identified through the
11*4882a593Smuzhiyun``id`` field. If unused, then the ``id`` is 0. So an event is uniquely
12*4882a593Smuzhiyunidentified by the ``(type, id)`` tuple.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe :c:type:`v4l2_fh` struct has a list of subscribed events on its
15*4882a593Smuzhiyun``subscribed`` field.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunWhen the user subscribes to an event, a :c:type:`v4l2_subscribed_event`
18*4882a593Smuzhiyunstruct is added to :c:type:`v4l2_fh`\ ``.subscribed``, one for every
19*4882a593Smuzhiyunsubscribed event.
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunEach :c:type:`v4l2_subscribed_event` struct ends with a
22*4882a593Smuzhiyun:c:type:`v4l2_kevent` ringbuffer, with the size given by the caller
23*4882a593Smuzhiyunof :c:func:`v4l2_event_subscribe`. This ringbuffer is used to store any events
24*4882a593Smuzhiyunraised by the driver.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunSo every ``(type, ID)`` event tuple will have its own
27*4882a593Smuzhiyun:c:type:`v4l2_kevent` ringbuffer. This guarantees that if a driver is
28*4882a593Smuzhiyungenerating lots of events of one type in a short time, then that will
29*4882a593Smuzhiyunnot overwrite events of another type.
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunBut if you get more events of one type than the size of the
32*4882a593Smuzhiyun:c:type:`v4l2_kevent` ringbuffer, then the oldest event will be dropped
33*4882a593Smuzhiyunand the new one added.
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunThe :c:type:`v4l2_kevent` struct links into the ``available``
36*4882a593Smuzhiyunlist of the :c:type:`v4l2_fh` struct so :ref:`VIDIOC_DQEVENT` will
37*4882a593Smuzhiyunknow which event to dequeue first.
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunFinally, if the event subscription is associated with a particular object
40*4882a593Smuzhiyunsuch as a V4L2 control, then that object needs to know about that as well
41*4882a593Smuzhiyunso that an event can be raised by that object. So the ``node`` field can
42*4882a593Smuzhiyunbe used to link the :c:type:`v4l2_subscribed_event` struct into a list of
43*4882a593Smuzhiyunsuch objects.
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunSo to summarize:
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun- struct v4l2_fh has two lists: one of the ``subscribed`` events,
48*4882a593Smuzhiyun  and one of the ``available`` events.
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun- struct v4l2_subscribed_event has a ringbuffer of raised
51*4882a593Smuzhiyun  (pending) events of that particular type.
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun- If struct v4l2_subscribed_event is associated with a specific
54*4882a593Smuzhiyun  object, then that object will have an internal list of
55*4882a593Smuzhiyun  struct v4l2_subscribed_event so it knows who subscribed an
56*4882a593Smuzhiyun  event to that object.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunFurthermore, the internal struct v4l2_subscribed_event has
59*4882a593Smuzhiyun``merge()`` and ``replace()`` callbacks which drivers can set. These
60*4882a593Smuzhiyuncallbacks are called when a new event is raised and there is no more room.
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunThe ``replace()`` callback allows you to replace the payload of the old event
63*4882a593Smuzhiyunwith that of the new event, merging any relevant data from the old payload
64*4882a593Smuzhiyuninto the new payload that replaces it. It is called when this event type has
65*4882a593Smuzhiyuna ringbuffer with size is one, i.e. only one event can be stored in the
66*4882a593Smuzhiyunringbuffer.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunThe ``merge()`` callback allows you to merge the oldest event payload into
69*4882a593Smuzhiyunthat of the second-oldest event payload. It is called when
70*4882a593Smuzhiyunthe ringbuffer has size is greater than one.
71*4882a593Smuzhiyun
72*4882a593SmuzhiyunThis way no status information is lost, just the intermediate steps leading
73*4882a593Smuzhiyunup to that state.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunA good example of these ``replace``/``merge`` callbacks is in v4l2-event.c:
76*4882a593Smuzhiyun``ctrls_replace()`` and ``ctrls_merge()`` callbacks for the control event.
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun.. note::
79*4882a593Smuzhiyun	these callbacks can be called from interrupt context, so they must
80*4882a593Smuzhiyun	be fast.
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunIn order to queue events to video device, drivers should call:
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun	:c:func:`v4l2_event_queue <v4l2_event_queue>`
85*4882a593Smuzhiyun	(:c:type:`vdev <video_device>`, :c:type:`ev <v4l2_event>`)
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunThe driver's only responsibility is to fill in the type and the data fields.
88*4882a593SmuzhiyunThe other fields will be filled in by V4L2.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunEvent subscription
91*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunSubscribing to an event is via:
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun	:c:func:`v4l2_event_subscribe <v4l2_event_subscribe>`
96*4882a593Smuzhiyun	(:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>` ,
97*4882a593Smuzhiyun	elems, :c:type:`ops <v4l2_subscribed_event_ops>`)
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunThis function is used to implement :c:type:`video_device`->
101*4882a593Smuzhiyun:c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_subscribe_event``,
102*4882a593Smuzhiyunbut the driver must check first if the driver is able to produce events
103*4882a593Smuzhiyunwith specified event id, and then should call
104*4882a593Smuzhiyun:c:func:`v4l2_event_subscribe` to subscribe the event.
105*4882a593Smuzhiyun
106*4882a593SmuzhiyunThe elems argument is the size of the event queue for this event. If it is 0,
107*4882a593Smuzhiyunthen the framework will fill in a default value (this depends on the event
108*4882a593Smuzhiyuntype).
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunThe ops argument allows the driver to specify a number of callbacks:
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun.. tabularcolumns:: |p{1.5cm}|p{16.0cm}|
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun======== ==============================================================
115*4882a593SmuzhiyunCallback Description
116*4882a593Smuzhiyun======== ==============================================================
117*4882a593Smuzhiyunadd      called when a new listener gets added (subscribing to the same
118*4882a593Smuzhiyun	 event twice will only cause this callback to get called once)
119*4882a593Smuzhiyundel      called when a listener stops listening
120*4882a593Smuzhiyunreplace  replace event 'old' with event 'new'.
121*4882a593Smuzhiyunmerge    merge event 'old' into event 'new'.
122*4882a593Smuzhiyun======== ==============================================================
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunAll 4 callbacks are optional, if you don't want to specify any callbacks
125*4882a593Smuzhiyunthe ops argument itself maybe ``NULL``.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunUnsubscribing an event
128*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunUnsubscribing to an event is via:
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun	:c:func:`v4l2_event_unsubscribe <v4l2_event_unsubscribe>`
133*4882a593Smuzhiyun	(:c:type:`fh <v4l2_fh>`, :c:type:`sub <v4l2_event_subscription>`)
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunThis function is used to implement :c:type:`video_device`->
136*4882a593Smuzhiyun:c:type:`ioctl_ops <v4l2_ioctl_ops>`-> ``vidioc_unsubscribe_event``.
137*4882a593SmuzhiyunA driver may call :c:func:`v4l2_event_unsubscribe` directly unless it
138*4882a593Smuzhiyunwants to be involved in unsubscription process.
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunThe special type ``V4L2_EVENT_ALL`` may be used to unsubscribe all events. The
141*4882a593Smuzhiyundrivers may want to handle this in a special way.
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunCheck if there's a pending event
144*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunChecking if there's a pending event is via:
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun	:c:func:`v4l2_event_pending <v4l2_event_pending>`
149*4882a593Smuzhiyun	(:c:type:`fh <v4l2_fh>`)
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunThis function returns the number of pending events. Useful when implementing
153*4882a593Smuzhiyunpoll.
154*4882a593Smuzhiyun
155*4882a593SmuzhiyunHow events work
156*4882a593Smuzhiyun~~~~~~~~~~~~~~~
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunEvents are delivered to user space through the poll system call. The driver
159*4882a593Smuzhiyuncan use :c:type:`v4l2_fh`->wait (a wait_queue_head_t) as the argument for
160*4882a593Smuzhiyun``poll_wait()``.
161*4882a593Smuzhiyun
162*4882a593SmuzhiyunThere are standard and private events. New standard events must use the
163*4882a593Smuzhiyunsmallest available event type. The drivers must allocate their events from
164*4882a593Smuzhiyuntheir own class starting from class base. Class base is
165*4882a593Smuzhiyun``V4L2_EVENT_PRIVATE_START`` + n * 1000 where n is the lowest available number.
166*4882a593SmuzhiyunThe first event type in the class is reserved for future use, so the first
167*4882a593Smuzhiyunavailable event type is 'class base + 1'.
168*4882a593Smuzhiyun
169*4882a593SmuzhiyunAn example on how the V4L2 events may be used can be found in the OMAP
170*4882a593Smuzhiyun3 ISP driver (``drivers/media/platform/omap3isp``).
171*4882a593Smuzhiyun
172*4882a593SmuzhiyunA subdev can directly send an event to the :c:type:`v4l2_device` notify
173*4882a593Smuzhiyunfunction with ``V4L2_DEVICE_NOTIFY_EVENT``. This allows the bridge to map
174*4882a593Smuzhiyunthe subdev that sends the event to the video node(s) associated with the
175*4882a593Smuzhiyunsubdev that need to be informed about such an event.
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunV4L2 event functions and data structures
178*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun.. kernel-doc:: include/media/v4l2-event.h
181*4882a593Smuzhiyun
182