[media] doc-rst: move v4l2-dev doc to a separate file
[deliverable/linux.git] / Documentation / media / kapi / v4l2-framework.rst
1 Overview of the V4L2 driver framework
2 =====================================
3
4 This text documents the various structures provided by the V4L2 framework and
5 their relationships.
6
7
8 Introduction
9 ------------
10
11 The V4L2 drivers tend to be very complex due to the complexity of the
12 hardware: most devices have multiple ICs, export multiple device nodes in
13 /dev, and create also non-V4L2 devices such as DVB, ALSA, FB, I2C and input
14 (IR) devices.
15
16 Especially the fact that V4L2 drivers have to setup supporting ICs to
17 do audio/video muxing/encoding/decoding makes it more complex than most.
18 Usually these ICs are connected to the main bridge driver through one or
19 more I2C busses, but other busses can also be used. Such devices are
20 called 'sub-devices'.
21
22 For a long time the framework was limited to the video_device struct for
23 creating V4L device nodes and video_buf for handling the video buffers
24 (note that this document does not discuss the video_buf framework).
25
26 This meant that all drivers had to do the setup of device instances and
27 connecting to sub-devices themselves. Some of this is quite complicated
28 to do right and many drivers never did do it correctly.
29
30 There is also a lot of common code that could never be refactored due to
31 the lack of a framework.
32
33 So this framework sets up the basic building blocks that all drivers
34 need and this same framework should make it much easier to refactor
35 common code into utility functions shared by all drivers.
36
37 A good example to look at as a reference is the v4l2-pci-skeleton.c
38 source that is available in samples/v4l/. It is a skeleton driver for
39 a PCI capture card, and demonstrates how to use the V4L2 driver
40 framework. It can be used as a template for real PCI video capture driver.
41
42 Structure of a driver
43 ---------------------
44
45 All drivers have the following structure:
46
47 1) A struct for each device instance containing the device state.
48
49 2) A way of initializing and commanding sub-devices (if any).
50
51 3) Creating V4L2 device nodes (/dev/videoX, /dev/vbiX and /dev/radioX)
52 and keeping track of device-node specific data.
53
54 4) Filehandle-specific structs containing per-filehandle data;
55
56 5) video buffer handling.
57
58 This is a rough schematic of how it all relates:
59
60 .. code-block:: none
61
62 device instances
63 |
64 +-sub-device instances
65 |
66 \-V4L2 device nodes
67 |
68 \-filehandle instances
69
70
71 Structure of the framework
72 --------------------------
73
74 The framework closely resembles the driver structure: it has a v4l2_device
75 struct for the device instance data, a v4l2_subdev struct to refer to
76 sub-device instances, the video_device struct stores V4L2 device node data
77 and the v4l2_fh struct keeps track of filehandle instances.
78
79 The V4L2 framework also optionally integrates with the media framework. If a
80 driver sets the struct v4l2_device mdev field, sub-devices and video nodes
81 will automatically appear in the media framework as entities.
82
83
84
85 video buffer helper functions
86 -----------------------------
87
88 The v4l2 core API provides a set of standard methods (called "videobuf")
89 for dealing with video buffers. Those methods allow a driver to implement
90 read(), mmap() and overlay() in a consistent way. There are currently
91 methods for using video buffers on devices that supports DMA with
92 scatter/gather method (videobuf-dma-sg), DMA with linear access
93 (videobuf-dma-contig), and vmalloced buffers, mostly used on USB drivers
94 (videobuf-vmalloc).
95
96 Please see Documentation/video4linux/videobuf for more information on how
97 to use the videobuf layer.
98
99 struct v4l2_fh
100 --------------
101
102 struct v4l2_fh provides a way to easily keep file handle specific data
103 that is used by the V4L2 framework. New drivers must use struct v4l2_fh
104 since it is also used to implement priority handling (VIDIOC_G/S_PRIORITY).
105
106 The users of v4l2_fh (in the V4L2 framework, not the driver) know
107 whether a driver uses v4l2_fh as its file->private_data pointer by
108 testing the V4L2_FL_USES_V4L2_FH bit in video_device->flags. This bit is
109 set whenever v4l2_fh_init() is called.
110
111 struct v4l2_fh is allocated as a part of the driver's own file handle
112 structure and file->private_data is set to it in the driver's open
113 function by the driver.
114
115 In many cases the struct v4l2_fh will be embedded in a larger structure.
116 In that case you should call v4l2_fh_init+v4l2_fh_add in open() and
117 v4l2_fh_del+v4l2_fh_exit in release().
118
119 Drivers can extract their own file handle structure by using the container_of
120 macro. Example:
121
122 .. code-block:: none
123
124 struct my_fh {
125 int blah;
126 struct v4l2_fh fh;
127 };
128
129 ...
130
131 int my_open(struct file *file)
132 {
133 struct my_fh *my_fh;
134 struct video_device *vfd;
135 int ret;
136
137 ...
138
139 my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
140
141 ...
142
143 v4l2_fh_init(&my_fh->fh, vfd);
144
145 ...
146
147 file->private_data = &my_fh->fh;
148 v4l2_fh_add(&my_fh->fh);
149 return 0;
150 }
151
152 int my_release(struct file *file)
153 {
154 struct v4l2_fh *fh = file->private_data;
155 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
156
157 ...
158 v4l2_fh_del(&my_fh->fh);
159 v4l2_fh_exit(&my_fh->fh);
160 kfree(my_fh);
161 return 0;
162 }
163
164 Below is a short description of the v4l2_fh functions used:
165
166 .. code-block:: none
167
168 void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)
169
170 Initialise the file handle. This *MUST* be performed in the driver's
171 v4l2_file_operations->open() handler.
172
173 .. code-block:: none
174
175 void v4l2_fh_add(struct v4l2_fh *fh)
176
177 Add a v4l2_fh to video_device file handle list. Must be called once the
178 file handle is completely initialized.
179
180 .. code-block:: none
181
182 void v4l2_fh_del(struct v4l2_fh *fh)
183
184 Unassociate the file handle from video_device(). The file handle
185 exit function may now be called.
186
187 .. code-block:: none
188
189 void v4l2_fh_exit(struct v4l2_fh *fh)
190
191 Uninitialise the file handle. After uninitialisation the v4l2_fh
192 memory can be freed.
193
194
195 If struct v4l2_fh is not embedded, then you can use these helper functions:
196
197 .. code-block:: none
198
199 int v4l2_fh_open(struct file *filp)
200
201 This allocates a struct v4l2_fh, initializes it and adds it to the struct
202 video_device associated with the file struct.
203
204 .. code-block:: none
205
206 int v4l2_fh_release(struct file *filp)
207
208 This deletes it from the struct video_device associated with the file
209 struct, uninitialised the v4l2_fh and frees it.
210
211 These two functions can be plugged into the v4l2_file_operation's open() and
212 release() ops.
213
214
215 Several drivers need to do something when the first file handle is opened and
216 when the last file handle closes. Two helper functions were added to check
217 whether the v4l2_fh struct is the only open filehandle of the associated
218 device node:
219
220 .. code-block:: none
221
222 int v4l2_fh_is_singular(struct v4l2_fh *fh)
223
224 Returns 1 if the file handle is the only open file handle, else 0.
225
226 .. code-block:: none
227
228 int v4l2_fh_is_singular_file(struct file *filp)
229
230 Same, but it calls v4l2_fh_is_singular with filp->private_data.
231
232
233 V4L2 events
234 -----------
235
236 The V4L2 events provide a generic way to pass events to user space.
237 The driver must use v4l2_fh to be able to support V4L2 events.
238
239 Events are defined by a type and an optional ID. The ID may refer to a V4L2
240 object such as a control ID. If unused, then the ID is 0.
241
242 When the user subscribes to an event the driver will allocate a number of
243 kevent structs for that event. So every (type, ID) event tuple will have
244 its own set of kevent structs. This guarantees that if a driver is generating
245 lots of events of one type in a short time, then that will not overwrite
246 events of another type.
247
248 But if you get more events of one type than the number of kevents that were
249 reserved, then the oldest event will be dropped and the new one added.
250
251 Furthermore, the internal struct v4l2_subscribed_event has merge() and
252 replace() callbacks which drivers can set. These callbacks are called when
253 a new event is raised and there is no more room. The replace() callback
254 allows you to replace the payload of the old event with that of the new event,
255 merging any relevant data from the old payload into the new payload that
256 replaces it. It is called when this event type has only one kevent struct
257 allocated. The merge() callback allows you to merge the oldest event payload
258 into that of the second-oldest event payload. It is called when there are two
259 or more kevent structs allocated.
260
261 This way no status information is lost, just the intermediate steps leading
262 up to that state.
263
264 A good example of these replace/merge callbacks is in v4l2-event.c:
265 ctrls_replace() and ctrls_merge() callbacks for the control event.
266
267 Note: these callbacks can be called from interrupt context, so they must be
268 fast.
269
270 Useful functions:
271
272 .. code-block:: none
273
274 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
275
276 Queue events to video device. The driver's only responsibility is to fill
277 in the type and the data fields. The other fields will be filled in by
278 V4L2.
279
280 .. code-block:: none
281
282 int v4l2_event_subscribe(struct v4l2_fh *fh,
283 struct v4l2_event_subscription *sub, unsigned elems,
284 const struct v4l2_subscribed_event_ops *ops)
285
286 The video_device->ioctl_ops->vidioc_subscribe_event must check the driver
287 is able to produce events with specified event id. Then it calls
288 v4l2_event_subscribe() to subscribe the event.
289
290 The elems argument is the size of the event queue for this event. If it is 0,
291 then the framework will fill in a default value (this depends on the event
292 type).
293
294 The ops argument allows the driver to specify a number of callbacks:
295 * add: called when a new listener gets added (subscribing to the same
296 event twice will only cause this callback to get called once)
297 * del: called when a listener stops listening
298 * replace: replace event 'old' with event 'new'.
299 * merge: merge event 'old' into event 'new'.
300 All 4 callbacks are optional, if you don't want to specify any callbacks
301 the ops argument itself maybe NULL.
302
303 .. code-block:: none
304
305 int v4l2_event_unsubscribe(struct v4l2_fh *fh,
306 struct v4l2_event_subscription *sub)
307
308 vidioc_unsubscribe_event in struct v4l2_ioctl_ops. A driver may use
309 v4l2_event_unsubscribe() directly unless it wants to be involved in
310 unsubscription process.
311
312 The special type V4L2_EVENT_ALL may be used to unsubscribe all events. The
313 drivers may want to handle this in a special way.
314
315 .. code-block:: none
316
317 int v4l2_event_pending(struct v4l2_fh *fh)
318
319 Returns the number of pending events. Useful when implementing poll.
320
321 Events are delivered to user space through the poll system call. The driver
322 can use v4l2_fh->wait (a wait_queue_head_t) as the argument for poll_wait().
323
324 There are standard and private events. New standard events must use the
325 smallest available event type. The drivers must allocate their events from
326 their own class starting from class base. Class base is
327 V4L2_EVENT_PRIVATE_START + n * 1000 where n is the lowest available number.
328 The first event type in the class is reserved for future use, so the first
329 available event type is 'class base + 1'.
330
331 An example on how the V4L2 events may be used can be found in the OMAP
332 3 ISP driver (drivers/media/platform/omap3isp).
333
334 A subdev can directly send an event to the v4l2_device notify function with
335 V4L2_DEVICE_NOTIFY_EVENT. This allows the bridge to map the subdev that sends
336 the event to the video node(s) associated with the subdev that need to be
337 informed about such an event.
338
339 V4L2 clocks
340 -----------
341
342 Many subdevices, like camera sensors, TV decoders and encoders, need a clock
343 signal to be supplied by the system. Often this clock is supplied by the
344 respective bridge device. The Linux kernel provides a Common Clock Framework for
345 this purpose. However, it is not (yet) available on all architectures. Besides,
346 the nature of the multi-functional (clock, data + synchronisation, I2C control)
347 connection of subdevices to the system might impose special requirements on the
348 clock API usage. E.g. V4L2 has to support clock provider driver unregistration
349 while a subdevice driver is holding a reference to the clock. For these reasons
350 a V4L2 clock helper API has been developed and is provided to bridge and
351 subdevice drivers.
352
353 The API consists of two parts: two functions to register and unregister a V4L2
354 clock source: v4l2_clk_register() and v4l2_clk_unregister() and calls to control
355 a clock object, similar to the respective generic clock API calls:
356 v4l2_clk_get(), v4l2_clk_put(), v4l2_clk_enable(), v4l2_clk_disable(),
357 v4l2_clk_get_rate(), and v4l2_clk_set_rate(). Clock suppliers have to provide
358 clock operations that will be called when clock users invoke respective API
359 methods.
360
361 It is expected that once the CCF becomes available on all relevant
362 architectures this API will be removed.
This page took 0.038741 seconds and 6 git commands to generate.