xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/media/v4l2-videobuf.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun.. _vb_framework:
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunVideobuf Framework
6*4882a593Smuzhiyun==================
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunAuthor: Jonathan Corbet <corbet@lwn.net>
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunCurrent as of 2.6.33
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun.. note::
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun   The videobuf framework was deprecated in favor of videobuf2. Shouldn't
15*4882a593Smuzhiyun   be used on new drivers.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunIntroduction
18*4882a593Smuzhiyun------------
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunThe videobuf layer functions as a sort of glue layer between a V4L2 driver
21*4882a593Smuzhiyunand user space.  It handles the allocation and management of buffers for
22*4882a593Smuzhiyunthe storage of video frames.  There is a set of functions which can be used
23*4882a593Smuzhiyunto implement many of the standard POSIX I/O system calls, including read(),
24*4882a593Smuzhiyunpoll(), and, happily, mmap().  Another set of functions can be used to
25*4882a593Smuzhiyunimplement the bulk of the V4L2 ioctl() calls related to streaming I/O,
26*4882a593Smuzhiyunincluding buffer allocation, queueing and dequeueing, and streaming
27*4882a593Smuzhiyuncontrol.  Using videobuf imposes a few design decisions on the driver
28*4882a593Smuzhiyunauthor, but the payback comes in the form of reduced code in the driver and
29*4882a593Smuzhiyuna consistent implementation of the V4L2 user-space API.
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunBuffer types
32*4882a593Smuzhiyun------------
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunNot all video devices use the same kind of buffers.  In fact, there are (at
35*4882a593Smuzhiyunleast) three common variations:
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun - Buffers which are scattered in both the physical and (kernel) virtual
38*4882a593Smuzhiyun   address spaces.  (Almost) all user-space buffers are like this, but it
39*4882a593Smuzhiyun   makes great sense to allocate kernel-space buffers this way as well when
40*4882a593Smuzhiyun   it is possible.  Unfortunately, it is not always possible; working with
41*4882a593Smuzhiyun   this kind of buffer normally requires hardware which can do
42*4882a593Smuzhiyun   scatter/gather DMA operations.
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun - Buffers which are physically scattered, but which are virtually
45*4882a593Smuzhiyun   contiguous; buffers allocated with vmalloc(), in other words.  These
46*4882a593Smuzhiyun   buffers are just as hard to use for DMA operations, but they can be
47*4882a593Smuzhiyun   useful in situations where DMA is not available but virtually-contiguous
48*4882a593Smuzhiyun   buffers are convenient.
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun - Buffers which are physically contiguous.  Allocation of this kind of
51*4882a593Smuzhiyun   buffer can be unreliable on fragmented systems, but simpler DMA
52*4882a593Smuzhiyun   controllers cannot deal with anything else.
53*4882a593Smuzhiyun
54*4882a593SmuzhiyunVideobuf can work with all three types of buffers, but the driver author
55*4882a593Smuzhiyunmust pick one at the outset and design the driver around that decision.
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun[It's worth noting that there's a fourth kind of buffer: "overlay" buffers
58*4882a593Smuzhiyunwhich are located within the system's video memory.  The overlay
59*4882a593Smuzhiyunfunctionality is considered to be deprecated for most use, but it still
60*4882a593Smuzhiyunshows up occasionally in system-on-chip drivers where the performance
61*4882a593Smuzhiyunbenefits merit the use of this technique.  Overlay buffers can be handled
62*4882a593Smuzhiyunas a form of scattered buffer, but there are very few implementations in
63*4882a593Smuzhiyunthe kernel and a description of this technique is currently beyond the
64*4882a593Smuzhiyunscope of this document.]
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunData structures, callbacks, and initialization
67*4882a593Smuzhiyun----------------------------------------------
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunDepending on which type of buffers are being used, the driver should
70*4882a593Smuzhiyuninclude one of the following files:
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun.. code-block:: none
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun    <media/videobuf-dma-sg.h>		/* Physically scattered */
75*4882a593Smuzhiyun    <media/videobuf-vmalloc.h>		/* vmalloc() buffers	*/
76*4882a593Smuzhiyun    <media/videobuf-dma-contig.h>	/* Physically contiguous */
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunThe driver's data structure describing a V4L2 device should include a
79*4882a593Smuzhiyunstruct videobuf_queue instance for the management of the buffer queue,
80*4882a593Smuzhiyunalong with a list_head for the queue of available buffers.  There will also
81*4882a593Smuzhiyunneed to be an interrupt-safe spinlock which is used to protect (at least)
82*4882a593Smuzhiyunthe queue.
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunThe next step is to write four simple callbacks to help videobuf deal with
85*4882a593Smuzhiyunthe management of buffers:
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun.. code-block:: none
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun    struct videobuf_queue_ops {
90*4882a593Smuzhiyun	int (*buf_setup)(struct videobuf_queue *q,
91*4882a593Smuzhiyun			 unsigned int *count, unsigned int *size);
92*4882a593Smuzhiyun	int (*buf_prepare)(struct videobuf_queue *q,
93*4882a593Smuzhiyun			   struct videobuf_buffer *vb,
94*4882a593Smuzhiyun			   enum v4l2_field field);
95*4882a593Smuzhiyun	void (*buf_queue)(struct videobuf_queue *q,
96*4882a593Smuzhiyun			  struct videobuf_buffer *vb);
97*4882a593Smuzhiyun	void (*buf_release)(struct videobuf_queue *q,
98*4882a593Smuzhiyun			    struct videobuf_buffer *vb);
99*4882a593Smuzhiyun    };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyunbuf_setup() is called early in the I/O process, when streaming is being
102*4882a593Smuzhiyuninitiated; its purpose is to tell videobuf about the I/O stream.  The count
103*4882a593Smuzhiyunparameter will be a suggested number of buffers to use; the driver should
104*4882a593Smuzhiyuncheck it for rationality and adjust it if need be.  As a practical rule, a
105*4882a593Smuzhiyunminimum of two buffers are needed for proper streaming, and there is
106*4882a593Smuzhiyunusually a maximum (which cannot exceed 32) which makes sense for each
107*4882a593Smuzhiyundevice.  The size parameter should be set to the expected (maximum) size
108*4882a593Smuzhiyunfor each frame of data.
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunEach buffer (in the form of a struct videobuf_buffer pointer) will be
111*4882a593Smuzhiyunpassed to buf_prepare(), which should set the buffer's size, width, height,
112*4882a593Smuzhiyunand field fields properly.  If the buffer's state field is
113*4882a593SmuzhiyunVIDEOBUF_NEEDS_INIT, the driver should pass it to:
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun.. code-block:: none
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun    int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
118*4882a593Smuzhiyun			struct v4l2_framebuffer *fbuf);
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunAmong other things, this call will usually allocate memory for the buffer.
121*4882a593SmuzhiyunFinally, the buf_prepare() function should set the buffer's state to
122*4882a593SmuzhiyunVIDEOBUF_PREPARED.
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunWhen a buffer is queued for I/O, it is passed to buf_queue(), which should
125*4882a593Smuzhiyunput it onto the driver's list of available buffers and set its state to
126*4882a593SmuzhiyunVIDEOBUF_QUEUED.  Note that this function is called with the queue spinlock
127*4882a593Smuzhiyunheld; if it tries to acquire it as well things will come to a screeching
128*4882a593Smuzhiyunhalt.  Yes, this is the voice of experience.  Note also that videobuf may
129*4882a593Smuzhiyunwait on the first buffer in the queue; placing other buffers in front of it
130*4882a593Smuzhiyuncould again gum up the works.  So use list_add_tail() to enqueue buffers.
131*4882a593Smuzhiyun
132*4882a593SmuzhiyunFinally, buf_release() is called when a buffer is no longer intended to be
133*4882a593Smuzhiyunused.  The driver should ensure that there is no I/O active on the buffer,
134*4882a593Smuzhiyunthen pass it to the appropriate free routine(s):
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun.. code-block:: none
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun    /* Scatter/gather drivers */
139*4882a593Smuzhiyun    int videobuf_dma_unmap(struct videobuf_queue *q,
140*4882a593Smuzhiyun			   struct videobuf_dmabuf *dma);
141*4882a593Smuzhiyun    int videobuf_dma_free(struct videobuf_dmabuf *dma);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun    /* vmalloc drivers */
144*4882a593Smuzhiyun    void videobuf_vmalloc_free (struct videobuf_buffer *buf);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun    /* Contiguous drivers */
147*4882a593Smuzhiyun    void videobuf_dma_contig_free(struct videobuf_queue *q,
148*4882a593Smuzhiyun				  struct videobuf_buffer *buf);
149*4882a593Smuzhiyun
150*4882a593SmuzhiyunOne way to ensure that a buffer is no longer under I/O is to pass it to:
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun.. code-block:: none
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun    int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
155*4882a593Smuzhiyun
156*4882a593SmuzhiyunHere, vb is the buffer, non_blocking indicates whether non-blocking I/O
157*4882a593Smuzhiyunshould be used (it should be zero in the buf_release() case), and intr
158*4882a593Smuzhiyuncontrols whether an interruptible wait is used.
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunFile operations
161*4882a593Smuzhiyun---------------
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunAt this point, much of the work is done; much of the rest is slipping
164*4882a593Smuzhiyunvideobuf calls into the implementation of the other driver callbacks.  The
165*4882a593Smuzhiyunfirst step is in the open() function, which must initialize the
166*4882a593Smuzhiyunvideobuf queue.  The function to use depends on the type of buffer used:
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun.. code-block:: none
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun    void videobuf_queue_sg_init(struct videobuf_queue *q,
171*4882a593Smuzhiyun				struct videobuf_queue_ops *ops,
172*4882a593Smuzhiyun				struct device *dev,
173*4882a593Smuzhiyun				spinlock_t *irqlock,
174*4882a593Smuzhiyun				enum v4l2_buf_type type,
175*4882a593Smuzhiyun				enum v4l2_field field,
176*4882a593Smuzhiyun				unsigned int msize,
177*4882a593Smuzhiyun				void *priv);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun    void videobuf_queue_vmalloc_init(struct videobuf_queue *q,
180*4882a593Smuzhiyun				struct videobuf_queue_ops *ops,
181*4882a593Smuzhiyun				struct device *dev,
182*4882a593Smuzhiyun				spinlock_t *irqlock,
183*4882a593Smuzhiyun				enum v4l2_buf_type type,
184*4882a593Smuzhiyun				enum v4l2_field field,
185*4882a593Smuzhiyun				unsigned int msize,
186*4882a593Smuzhiyun				void *priv);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun    void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
189*4882a593Smuzhiyun				       struct videobuf_queue_ops *ops,
190*4882a593Smuzhiyun				       struct device *dev,
191*4882a593Smuzhiyun				       spinlock_t *irqlock,
192*4882a593Smuzhiyun				       enum v4l2_buf_type type,
193*4882a593Smuzhiyun				       enum v4l2_field field,
194*4882a593Smuzhiyun				       unsigned int msize,
195*4882a593Smuzhiyun				       void *priv);
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunIn each case, the parameters are the same: q is the queue structure for the
198*4882a593Smuzhiyundevice, ops is the set of callbacks as described above, dev is the device
199*4882a593Smuzhiyunstructure for this video device, irqlock is an interrupt-safe spinlock to
200*4882a593Smuzhiyunprotect access to the data structures, type is the buffer type used by the
201*4882a593Smuzhiyundevice (cameras will use V4L2_BUF_TYPE_VIDEO_CAPTURE, for example), field
202*4882a593Smuzhiyundescribes which field is being captured (often V4L2_FIELD_NONE for
203*4882a593Smuzhiyunprogressive devices), msize is the size of any containing structure used
204*4882a593Smuzhiyunaround struct videobuf_buffer, and priv is a private data pointer which
205*4882a593Smuzhiyunshows up in the priv_data field of struct videobuf_queue.  Note that these
206*4882a593Smuzhiyunare void functions which, evidently, are immune to failure.
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunV4L2 capture drivers can be written to support either of two APIs: the
209*4882a593Smuzhiyunread() system call and the rather more complicated streaming mechanism.  As
210*4882a593Smuzhiyuna general rule, it is necessary to support both to ensure that all
211*4882a593Smuzhiyunapplications have a chance of working with the device.  Videobuf makes it
212*4882a593Smuzhiyuneasy to do that with the same code.  To implement read(), the driver need
213*4882a593Smuzhiyunonly make a call to one of:
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun.. code-block:: none
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun    ssize_t videobuf_read_one(struct videobuf_queue *q,
218*4882a593Smuzhiyun			      char __user *data, size_t count,
219*4882a593Smuzhiyun			      loff_t *ppos, int nonblocking);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun    ssize_t videobuf_read_stream(struct videobuf_queue *q,
222*4882a593Smuzhiyun				 char __user *data, size_t count,
223*4882a593Smuzhiyun				 loff_t *ppos, int vbihack, int nonblocking);
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunEither one of these functions will read frame data into data, returning the
226*4882a593Smuzhiyunamount actually read; the difference is that videobuf_read_one() will only
227*4882a593Smuzhiyunread a single frame, while videobuf_read_stream() will read multiple frames
228*4882a593Smuzhiyunif they are needed to satisfy the count requested by the application.  A
229*4882a593Smuzhiyuntypical driver read() implementation will start the capture engine, call
230*4882a593Smuzhiyunone of the above functions, then stop the engine before returning (though a
231*4882a593Smuzhiyunsmarter implementation might leave the engine running for a little while in
232*4882a593Smuzhiyunanticipation of another read() call happening in the near future).
233*4882a593Smuzhiyun
234*4882a593SmuzhiyunThe poll() function can usually be implemented with a direct call to:
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun.. code-block:: none
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun    unsigned int videobuf_poll_stream(struct file *file,
239*4882a593Smuzhiyun				      struct videobuf_queue *q,
240*4882a593Smuzhiyun				      poll_table *wait);
241*4882a593Smuzhiyun
242*4882a593SmuzhiyunNote that the actual wait queue eventually used will be the one associated
243*4882a593Smuzhiyunwith the first available buffer.
244*4882a593Smuzhiyun
245*4882a593SmuzhiyunWhen streaming I/O is done to kernel-space buffers, the driver must support
246*4882a593Smuzhiyunthe mmap() system call to enable user space to access the data.  In many
247*4882a593SmuzhiyunV4L2 drivers, the often-complex mmap() implementation simplifies to a
248*4882a593Smuzhiyunsingle call to:
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun.. code-block:: none
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun    int videobuf_mmap_mapper(struct videobuf_queue *q,
253*4882a593Smuzhiyun			     struct vm_area_struct *vma);
254*4882a593Smuzhiyun
255*4882a593SmuzhiyunEverything else is handled by the videobuf code.
256*4882a593Smuzhiyun
257*4882a593SmuzhiyunThe release() function requires two separate videobuf calls:
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun.. code-block:: none
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun    void videobuf_stop(struct videobuf_queue *q);
262*4882a593Smuzhiyun    int videobuf_mmap_free(struct videobuf_queue *q);
263*4882a593Smuzhiyun
264*4882a593SmuzhiyunThe call to videobuf_stop() terminates any I/O in progress - though it is
265*4882a593Smuzhiyunstill up to the driver to stop the capture engine.  The call to
266*4882a593Smuzhiyunvideobuf_mmap_free() will ensure that all buffers have been unmapped; if
267*4882a593Smuzhiyunso, they will all be passed to the buf_release() callback.  If buffers
268*4882a593Smuzhiyunremain mapped, videobuf_mmap_free() returns an error code instead.  The
269*4882a593Smuzhiyunpurpose is clearly to cause the closing of the file descriptor to fail if
270*4882a593Smuzhiyunbuffers are still mapped, but every driver in the 2.6.32 kernel cheerfully
271*4882a593Smuzhiyunignores its return value.
272*4882a593Smuzhiyun
273*4882a593Smuzhiyunioctl() operations
274*4882a593Smuzhiyun------------------
275*4882a593Smuzhiyun
276*4882a593SmuzhiyunThe V4L2 API includes a very long list of driver callbacks to respond to
277*4882a593Smuzhiyunthe many ioctl() commands made available to user space.  A number of these
278*4882a593Smuzhiyun- those associated with streaming I/O - turn almost directly into videobuf
279*4882a593Smuzhiyuncalls.  The relevant helper functions are:
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun.. code-block:: none
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun    int videobuf_reqbufs(struct videobuf_queue *q,
284*4882a593Smuzhiyun			 struct v4l2_requestbuffers *req);
285*4882a593Smuzhiyun    int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
286*4882a593Smuzhiyun    int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b);
287*4882a593Smuzhiyun    int videobuf_dqbuf(struct videobuf_queue *q, struct v4l2_buffer *b,
288*4882a593Smuzhiyun		       int nonblocking);
289*4882a593Smuzhiyun    int videobuf_streamon(struct videobuf_queue *q);
290*4882a593Smuzhiyun    int videobuf_streamoff(struct videobuf_queue *q);
291*4882a593Smuzhiyun
292*4882a593SmuzhiyunSo, for example, a VIDIOC_REQBUFS call turns into a call to the driver's
293*4882a593Smuzhiyunvidioc_reqbufs() callback which, in turn, usually only needs to locate the
294*4882a593Smuzhiyunproper struct videobuf_queue pointer and pass it to videobuf_reqbufs().
295*4882a593SmuzhiyunThese support functions can replace a great deal of buffer management
296*4882a593Smuzhiyunboilerplate in a lot of V4L2 drivers.
297*4882a593Smuzhiyun
298*4882a593SmuzhiyunThe vidioc_streamon() and vidioc_streamoff() functions will be a bit more
299*4882a593Smuzhiyuncomplex, of course, since they will also need to deal with starting and
300*4882a593Smuzhiyunstopping the capture engine.
301*4882a593Smuzhiyun
302*4882a593SmuzhiyunBuffer allocation
303*4882a593Smuzhiyun-----------------
304*4882a593Smuzhiyun
305*4882a593SmuzhiyunThus far, we have talked about buffers, but have not looked at how they are
306*4882a593Smuzhiyunallocated.  The scatter/gather case is the most complex on this front.  For
307*4882a593Smuzhiyunallocation, the driver can leave buffer allocation entirely up to the
308*4882a593Smuzhiyunvideobuf layer; in this case, buffers will be allocated as anonymous
309*4882a593Smuzhiyunuser-space pages and will be very scattered indeed.  If the application is
310*4882a593Smuzhiyunusing user-space buffers, no allocation is needed; the videobuf layer will
311*4882a593Smuzhiyuntake care of calling get_user_pages() and filling in the scatterlist array.
312*4882a593Smuzhiyun
313*4882a593SmuzhiyunIf the driver needs to do its own memory allocation, it should be done in
314*4882a593Smuzhiyunthe vidioc_reqbufs() function, *after* calling videobuf_reqbufs().  The
315*4882a593Smuzhiyunfirst step is a call to:
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun.. code-block:: none
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun    struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf);
320*4882a593Smuzhiyun
321*4882a593SmuzhiyunThe returned videobuf_dmabuf structure (defined in
322*4882a593Smuzhiyun<media/videobuf-dma-sg.h>) includes a couple of relevant fields:
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun.. code-block:: none
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun    struct scatterlist  *sglist;
327*4882a593Smuzhiyun    int                 sglen;
328*4882a593Smuzhiyun
329*4882a593SmuzhiyunThe driver must allocate an appropriately-sized scatterlist array and
330*4882a593Smuzhiyunpopulate it with pointers to the pieces of the allocated buffer; sglen
331*4882a593Smuzhiyunshould be set to the length of the array.
332*4882a593Smuzhiyun
333*4882a593SmuzhiyunDrivers using the vmalloc() method need not (and cannot) concern themselves
334*4882a593Smuzhiyunwith buffer allocation at all; videobuf will handle those details.  The
335*4882a593Smuzhiyunsame is normally true of contiguous-DMA drivers as well; videobuf will
336*4882a593Smuzhiyunallocate the buffers (with dma_alloc_coherent()) when it sees fit.  That
337*4882a593Smuzhiyunmeans that these drivers may be trying to do high-order allocations at any
338*4882a593Smuzhiyuntime, an operation which is not always guaranteed to work.  Some drivers
339*4882a593Smuzhiyunplay tricks by allocating DMA space at system boot time; videobuf does not
340*4882a593Smuzhiyuncurrently play well with those drivers.
341*4882a593Smuzhiyun
342*4882a593SmuzhiyunAs of 2.6.31, contiguous-DMA drivers can work with a user-supplied buffer,
343*4882a593Smuzhiyunas long as that buffer is physically contiguous.  Normal user-space
344*4882a593Smuzhiyunallocations will not meet that criterion, but buffers obtained from other
345*4882a593Smuzhiyunkernel drivers, or those contained within huge pages, will work with these
346*4882a593Smuzhiyundrivers.
347*4882a593Smuzhiyun
348*4882a593SmuzhiyunFilling the buffers
349*4882a593Smuzhiyun-------------------
350*4882a593Smuzhiyun
351*4882a593SmuzhiyunThe final part of a videobuf implementation has no direct callback - it's
352*4882a593Smuzhiyunthe portion of the code which actually puts frame data into the buffers,
353*4882a593Smuzhiyunusually in response to interrupts from the device.  For all types of
354*4882a593Smuzhiyundrivers, this process works approximately as follows:
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun - Obtain the next available buffer and make sure that somebody is actually
357*4882a593Smuzhiyun   waiting for it.
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun - Get a pointer to the memory and put video data there.
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun - Mark the buffer as done and wake up the process waiting for it.
362*4882a593Smuzhiyun
363*4882a593SmuzhiyunStep (1) above is done by looking at the driver-managed list_head structure
364*4882a593Smuzhiyun- the one which is filled in the buf_queue() callback.  Because starting
365*4882a593Smuzhiyunthe engine and enqueueing buffers are done in separate steps, it's possible
366*4882a593Smuzhiyunfor the engine to be running without any buffers available - in the
367*4882a593Smuzhiyunvmalloc() case especially.  So the driver should be prepared for the list
368*4882a593Smuzhiyunto be empty.  It is equally possible that nobody is yet interested in the
369*4882a593Smuzhiyunbuffer; the driver should not remove it from the list or fill it until a
370*4882a593Smuzhiyunprocess is waiting on it.  That test can be done by examining the buffer's
371*4882a593Smuzhiyundone field (a wait_queue_head_t structure) with waitqueue_active().
372*4882a593Smuzhiyun
373*4882a593SmuzhiyunA buffer's state should be set to VIDEOBUF_ACTIVE before being mapped for
374*4882a593SmuzhiyunDMA; that ensures that the videobuf layer will not try to do anything with
375*4882a593Smuzhiyunit while the device is transferring data.
376*4882a593Smuzhiyun
377*4882a593SmuzhiyunFor scatter/gather drivers, the needed memory pointers will be found in the
378*4882a593Smuzhiyunscatterlist structure described above.  Drivers using the vmalloc() method
379*4882a593Smuzhiyuncan get a memory pointer with:
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun.. code-block:: none
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun    void *videobuf_to_vmalloc(struct videobuf_buffer *buf);
384*4882a593Smuzhiyun
385*4882a593SmuzhiyunFor contiguous DMA drivers, the function to use is:
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun.. code-block:: none
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun    dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunThe contiguous DMA API goes out of its way to hide the kernel-space address
392*4882a593Smuzhiyunof the DMA buffer from drivers.
393*4882a593Smuzhiyun
394*4882a593SmuzhiyunThe final step is to set the size field of the relevant videobuf_buffer
395*4882a593Smuzhiyunstructure to the actual size of the captured image, set state to
396*4882a593SmuzhiyunVIDEOBUF_DONE, then call wake_up() on the done queue.  At this point, the
397*4882a593Smuzhiyunbuffer is owned by the videobuf layer and the driver should not touch it
398*4882a593Smuzhiyunagain.
399*4882a593Smuzhiyun
400*4882a593SmuzhiyunDevelopers who are interested in more information can go into the relevant
401*4882a593Smuzhiyunheader files; there are a few low-level functions declared there which have
402*4882a593Smuzhiyunnot been talked about here.  Note also that all of these calls are exported
403*4882a593SmuzhiyunGPL-only, so they will not be available to non-GPL kernel modules.
404