0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 .. _vb_framework:
0004
0005 Videobuf Framework
0006 ==================
0007
0008 Author: Jonathan Corbet <corbet@lwn.net>
0009
0010 Current as of 2.6.33
0011
0012 .. note::
0013
0014 The videobuf framework was deprecated in favor of videobuf2. Shouldn't
0015 be used on new drivers.
0016
0017 Introduction
0018 ------------
0019
0020 The videobuf layer functions as a sort of glue layer between a V4L2 driver
0021 and user space. It handles the allocation and management of buffers for
0022 the storage of video frames. There is a set of functions which can be used
0023 to implement many of the standard POSIX I/O system calls, including read(),
0024 poll(), and, happily, mmap(). Another set of functions can be used to
0025 implement the bulk of the V4L2 ioctl() calls related to streaming I/O,
0026 including buffer allocation, queueing and dequeueing, and streaming
0027 control. Using videobuf imposes a few design decisions on the driver
0028 author, but the payback comes in the form of reduced code in the driver and
0029 a consistent implementation of the V4L2 user-space API.
0030
0031 Buffer types
0032 ------------
0033
0034 Not all video devices use the same kind of buffers. In fact, there are (at
0035 least) three common variations:
0036
0037 - Buffers which are scattered in both the physical and (kernel) virtual
0038 address spaces. (Almost) all user-space buffers are like this, but it
0039 makes great sense to allocate kernel-space buffers this way as well when
0040 it is possible. Unfortunately, it is not always possible; working with
0041 this kind of buffer normally requires hardware which can do
0042 scatter/gather DMA operations.
0043
0044 - Buffers which are physically scattered, but which are virtually
0045 contiguous; buffers allocated with vmalloc(), in other words. These
0046 buffers are just as hard to use for DMA operations, but they can be
0047 useful in situations where DMA is not available but virtually-contiguous
0048 buffers are convenient.
0049
0050 - Buffers which are physically contiguous. Allocation of this kind of
0051 buffer can be unreliable on fragmented systems, but simpler DMA
0052 controllers cannot deal with anything else.
0053
0054 Videobuf can work with all three types of buffers, but the driver author
0055 must pick one at the outset and design the driver around that decision.
0056
0057 [It's worth noting that there's a fourth kind of buffer: "overlay" buffers
0058 which are located within the system's video memory. The overlay
0059 functionality is considered to be deprecated for most use, but it still
0060 shows up occasionally in system-on-chip drivers where the performance
0061 benefits merit the use of this technique. Overlay buffers can be handled
0062 as a form of scattered buffer, but there are very few implementations in
0063 the kernel and a description of this technique is currently beyond the
0064 scope of this document.]
0065
0066 Data structures, callbacks, and initialization
0067 ----------------------------------------------
0068
0069 Depending on which type of buffers are being used, the driver should
0070 include one of the following files:
0071
0072 .. code-block:: none
0073
0074 <media/videobuf-dma-sg.h> /* Physically scattered */
0075 <media/videobuf-vmalloc.h> /* vmalloc() buffers */
0076 <media/videobuf-dma-contig.h> /* Physically contiguous */
0077
0078 The driver's data structure describing a V4L2 device should include a
0079 struct videobuf_queue instance for the management of the buffer queue,
0080 along with a list_head for the queue of available buffers. There will also
0081 need to be an interrupt-safe spinlock which is used to protect (at least)
0082 the queue.
0083
0084 The next step is to write four simple callbacks to help videobuf deal with
0085 the management of buffers:
0086
0087 .. code-block:: none
0088
0089 struct videobuf_queue_ops {
0090 int (*buf_setup)(struct videobuf_queue *q,
0091 unsigned int *count, unsigned int *size);
0092 int (*buf_prepare)(struct videobuf_queue *q,
0093 struct videobuf_buffer *vb,
0094 enum v4l2_field field);
0095 void (*buf_queue)(struct videobuf_queue *q,
0096 struct videobuf_buffer *vb);
0097 void (*buf_release)(struct videobuf_queue *q,
0098 struct videobuf_buffer *vb);
0099 };
0100
0101 buf_setup() is called early in the I/O process, when streaming is being
0102 initiated; its purpose is to tell videobuf about the I/O stream. The count
0103 parameter will be a suggested number of buffers to use; the driver should
0104 check it for rationality and adjust it if need be. As a practical rule, a
0105 minimum of two buffers are needed for proper streaming, and there is
0106 usually a maximum (which cannot exceed 32) which makes sense for each
0107 device. The size parameter should be set to the expected (maximum) size
0108 for each frame of data.
0109
0110 Each buffer (in the form of a struct videobuf_buffer pointer) will be
0111 passed to buf_prepare(), which should set the buffer's size, width, height,
0112 and field fields properly. If the buffer's state field is
0113 VIDEOBUF_NEEDS_INIT, the driver should pass it to:
0114
0115 .. code-block:: none
0116
0117 int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
0118 struct v4l2_framebuffer *fbuf);
0119
0120 Among other things, this call will usually allocate memory for the buffer.
0121 Finally, the buf_prepare() function should set the buffer's state to
0122 VIDEOBUF_PREPARED.
0123
0124 When a buffer is queued for I/O, it is passed to buf_queue(), which should
0125 put it onto the driver's list of available buffers and set its state to
0126 VIDEOBUF_QUEUED. Note that this function is called with the queue spinlock
0127 held; if it tries to acquire it as well things will come to a screeching
0128 halt. Yes, this is the voice of experience. Note also that videobuf may
0129 wait on the first buffer in the queue; placing other buffers in front of it
0130 could again gum up the works. So use list_add_tail() to enqueue buffers.
0131
0132 Finally, buf_release() is called when a buffer is no longer intended to be
0133 used. The driver should ensure that there is no I/O active on the buffer,
0134 then pass it to the appropriate free routine(s):
0135
0136 .. code-block:: none
0137
0138 /* Scatter/gather drivers */
0139 int videobuf_dma_unmap(struct videobuf_queue *q,
0140 struct videobuf_dmabuf *dma);
0141 int videobuf_dma_free(struct videobuf_dmabuf *dma);
0142
0143 /* vmalloc drivers */
0144 void videobuf_vmalloc_free (struct videobuf_buffer *buf);
0145
0146 /* Contiguous drivers */
0147 void videobuf_dma_contig_free(struct videobuf_queue *q,
0148 struct videobuf_buffer *buf);
0149
0150 One way to ensure that a buffer is no longer under I/O is to pass it to:
0151
0152 .. code-block:: none
0153
0154 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
0155
0156 Here, vb is the buffer, non_blocking indicates whether non-blocking I/O
0157 should be used (it should be zero in the buf_release() case), and intr
0158 controls whether an interruptible wait is used.
0159
0160 File operations
0161 ---------------
0162
0163 At this point, much of the work is done; much of the rest is slipping
0164 videobuf calls into the implementation of the other driver callbacks. The
0165 first step is in the open() function, which must initialize the
0166 videobuf queue. The function to use depends on the type of buffer used:
0167
0168 .. code-block:: none
0169
0170 void videobuf_queue_sg_init(struct videobuf_queue *q,
0171 struct videobuf_queue_ops *ops,
0172 struct device *dev,
0173 spinlock_t *irqlock,
0174 enum v4l2_buf_type type,
0175 enum v4l2_field field,
0176 unsigned int msize,
0177 void *priv);
0178
0179 void videobuf_queue_vmalloc_init(struct videobuf_queue *q,
0180 struct videobuf_queue_ops *ops,
0181 struct device *dev,
0182 spinlock_t *irqlock,
0183 enum v4l2_buf_type type,
0184 enum v4l2_field field,
0185 unsigned int msize,
0186 void *priv);
0187
0188 void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
0189 struct videobuf_queue_ops *ops,
0190 struct device *dev,
0191 spinlock_t *irqlock,
0192 enum v4l2_buf_type type,
0193 enum v4l2_field field,
0194 unsigned int msize,
0195 void *priv);
0196
0197 In each case, the parameters are the same: q is the queue structure for the
0198 device, ops is the set of callbacks as described above, dev is the device
0199 structure for this video device, irqlock is an interrupt-safe spinlock to
0200 protect access to the data structures, type is the buffer type used by the
0201 device (cameras will use V4L2_BUF_TYPE_VIDEO_CAPTURE, for example), field
0202 describes which field is being captured (often V4L2_FIELD_NONE for
0203 progressive devices), msize is the size of any containing structure used
0204 around struct videobuf_buffer, and priv is a private data pointer which
0205 shows up in the priv_data field of struct videobuf_queue. Note that these
0206 are void functions which, evidently, are immune to failure.
0207
0208 V4L2 capture drivers can be written to support either of two APIs: the
0209 read() system call and the rather more complicated streaming mechanism. As
0210 a general rule, it is necessary to support both to ensure that all
0211 applications have a chance of working with the device. Videobuf makes it
0212 easy to do that with the same code. To implement read(), the driver need
0213 only make a call to one of:
0214
0215 .. code-block:: none
0216
0217 ssize_t videobuf_read_one(struct videobuf_queue *q,
0218 char __user *data, size_t count,
0219 loff_t *ppos, int nonblocking);
0220
0221 ssize_t videobuf_read_stream(struct videobuf_queue *q,
0222 char __user *data, size_t count,
0223 loff_t *ppos, int vbihack, int nonblocking);
0224
0225 Either one of these functions will read frame data into data, returning the
0226 amount actually read; the difference is that videobuf_read_one() will only
0227 read a single frame, while videobuf_read_stream() will read multiple frames
0228 if they are needed to satisfy the count requested by the application. A
0229 typical driver read() implementation will start the capture engine, call
0230 one of the above functions, then stop the engine before returning (though a
0231 smarter implementation might leave the engine running for a little while in
0232 anticipation of another read() call happening in the near future).
0233
0234 The poll() function can usually be implemented with a direct call to:
0235
0236 .. code-block:: none
0237
0238 unsigned int videobuf_poll_stream(struct file *file,
0239 struct videobuf_queue *q,
0240 poll_table *wait);
0241
0242 Note that the actual wait queue eventually used will be the one associated
0243 with the first available buffer.
0244
0245 When streaming I/O is done to kernel-space buffers, the driver must support
0246 the mmap() system call to enable user space to access the data. In many
0247 V4L2 drivers, the often-complex mmap() implementation simplifies to a
0248 single call to:
0249
0250 .. code-block:: none
0251
0252 int videobuf_mmap_mapper(struct videobuf_queue *q,
0253 struct vm_area_struct *vma);
0254
0255 Everything else is handled by the videobuf code.
0256
0257 The release() function requires two separate videobuf calls:
0258
0259 .. code-block:: none
0260
0261 void videobuf_stop(struct videobuf_queue *q);
0262 int videobuf_mmap_free(struct videobuf_queue *q);
0263
0264 The call to videobuf_stop() terminates any I/O in progress - though it is
0265 still up to the driver to stop the capture engine. The call to
0266 videobuf_mmap_free() will ensure that all buffers have been unmapped; if
0267 so, they will all be passed to the buf_release() callback. If buffers
0268 remain mapped, videobuf_mmap_free() returns an error code instead. The
0269 purpose is clearly to cause the closing of the file descriptor to fail if
0270 buffers are still mapped, but every driver in the 2.6.32 kernel cheerfully
0271 ignores its return value.
0272
0273 ioctl() operations
0274 ------------------
0275
0276 The V4L2 API includes a very long list of driver callbacks to respond to
0277 the many ioctl() commands made available to user space. A number of these
0278 - those associated with streaming I/O - turn almost directly into videobuf
0279 calls. The relevant helper functions are:
0280
0281 .. code-block:: none
0282
0283 int videobuf_reqbufs(struct videobuf_queue *q,
0284 struct v4l2_requestbuffers *req);
0285 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
0286 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b);
0287 int videobuf_dqbuf(struct videobuf_queue *q, struct v4l2_buffer *b,
0288 int nonblocking);
0289 int videobuf_streamon(struct videobuf_queue *q);
0290 int videobuf_streamoff(struct videobuf_queue *q);
0291
0292 So, for example, a VIDIOC_REQBUFS call turns into a call to the driver's
0293 vidioc_reqbufs() callback which, in turn, usually only needs to locate the
0294 proper struct videobuf_queue pointer and pass it to videobuf_reqbufs().
0295 These support functions can replace a great deal of buffer management
0296 boilerplate in a lot of V4L2 drivers.
0297
0298 The vidioc_streamon() and vidioc_streamoff() functions will be a bit more
0299 complex, of course, since they will also need to deal with starting and
0300 stopping the capture engine.
0301
0302 Buffer allocation
0303 -----------------
0304
0305 Thus far, we have talked about buffers, but have not looked at how they are
0306 allocated. The scatter/gather case is the most complex on this front. For
0307 allocation, the driver can leave buffer allocation entirely up to the
0308 videobuf layer; in this case, buffers will be allocated as anonymous
0309 user-space pages and will be very scattered indeed. If the application is
0310 using user-space buffers, no allocation is needed; the videobuf layer will
0311 take care of calling get_user_pages() and filling in the scatterlist array.
0312
0313 If the driver needs to do its own memory allocation, it should be done in
0314 the vidioc_reqbufs() function, *after* calling videobuf_reqbufs(). The
0315 first step is a call to:
0316
0317 .. code-block:: none
0318
0319 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf);
0320
0321 The returned videobuf_dmabuf structure (defined in
0322 <media/videobuf-dma-sg.h>) includes a couple of relevant fields:
0323
0324 .. code-block:: none
0325
0326 struct scatterlist *sglist;
0327 int sglen;
0328
0329 The driver must allocate an appropriately-sized scatterlist array and
0330 populate it with pointers to the pieces of the allocated buffer; sglen
0331 should be set to the length of the array.
0332
0333 Drivers using the vmalloc() method need not (and cannot) concern themselves
0334 with buffer allocation at all; videobuf will handle those details. The
0335 same is normally true of contiguous-DMA drivers as well; videobuf will
0336 allocate the buffers (with dma_alloc_coherent()) when it sees fit. That
0337 means that these drivers may be trying to do high-order allocations at any
0338 time, an operation which is not always guaranteed to work. Some drivers
0339 play tricks by allocating DMA space at system boot time; videobuf does not
0340 currently play well with those drivers.
0341
0342 As of 2.6.31, contiguous-DMA drivers can work with a user-supplied buffer,
0343 as long as that buffer is physically contiguous. Normal user-space
0344 allocations will not meet that criterion, but buffers obtained from other
0345 kernel drivers, or those contained within huge pages, will work with these
0346 drivers.
0347
0348 Filling the buffers
0349 -------------------
0350
0351 The final part of a videobuf implementation has no direct callback - it's
0352 the portion of the code which actually puts frame data into the buffers,
0353 usually in response to interrupts from the device. For all types of
0354 drivers, this process works approximately as follows:
0355
0356 - Obtain the next available buffer and make sure that somebody is actually
0357 waiting for it.
0358
0359 - Get a pointer to the memory and put video data there.
0360
0361 - Mark the buffer as done and wake up the process waiting for it.
0362
0363 Step (1) above is done by looking at the driver-managed list_head structure
0364 - the one which is filled in the buf_queue() callback. Because starting
0365 the engine and enqueueing buffers are done in separate steps, it's possible
0366 for the engine to be running without any buffers available - in the
0367 vmalloc() case especially. So the driver should be prepared for the list
0368 to be empty. It is equally possible that nobody is yet interested in the
0369 buffer; the driver should not remove it from the list or fill it until a
0370 process is waiting on it. That test can be done by examining the buffer's
0371 done field (a wait_queue_head_t structure) with waitqueue_active().
0372
0373 A buffer's state should be set to VIDEOBUF_ACTIVE before being mapped for
0374 DMA; that ensures that the videobuf layer will not try to do anything with
0375 it while the device is transferring data.
0376
0377 For scatter/gather drivers, the needed memory pointers will be found in the
0378 scatterlist structure described above. Drivers using the vmalloc() method
0379 can get a memory pointer with:
0380
0381 .. code-block:: none
0382
0383 void *videobuf_to_vmalloc(struct videobuf_buffer *buf);
0384
0385 For contiguous DMA drivers, the function to use is:
0386
0387 .. code-block:: none
0388
0389 dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
0390
0391 The contiguous DMA API goes out of its way to hide the kernel-space address
0392 of the DMA buffer from drivers.
0393
0394 The final step is to set the size field of the relevant videobuf_buffer
0395 structure to the actual size of the captured image, set state to
0396 VIDEOBUF_DONE, then call wake_up() on the done queue. At this point, the
0397 buffer is owned by the videobuf layer and the driver should not touch it
0398 again.
0399
0400 Developers who are interested in more information can go into the relevant
0401 header files; there are a few low-level functions declared there which have
0402 not been talked about here. Note also that all of these calls are exported
0403 GPL-only, so they will not be available to non-GPL kernel modules.