1ff768f59SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0 2ff768f59SMauro Carvalho Chehab 3ff768f59SMauro Carvalho Chehab.. _vb_framework: 4ff768f59SMauro Carvalho Chehab 5ff768f59SMauro Carvalho ChehabVideobuf Framework 6ff768f59SMauro Carvalho Chehab================== 7ff768f59SMauro Carvalho Chehab 8ff768f59SMauro Carvalho ChehabAuthor: Jonathan Corbet <corbet@lwn.net> 9ff768f59SMauro Carvalho Chehab 10ff768f59SMauro Carvalho ChehabCurrent as of 2.6.33 11ff768f59SMauro Carvalho Chehab 12ff768f59SMauro Carvalho Chehab.. note:: 13ff768f59SMauro Carvalho Chehab 14ff768f59SMauro Carvalho Chehab The videobuf framework was deprecated in favor of videobuf2. Shouldn't 15ff768f59SMauro Carvalho Chehab be used on new drivers. 16ff768f59SMauro Carvalho Chehab 17ff768f59SMauro Carvalho ChehabIntroduction 18ff768f59SMauro Carvalho Chehab------------ 19ff768f59SMauro Carvalho Chehab 20ff768f59SMauro Carvalho ChehabThe videobuf layer functions as a sort of glue layer between a V4L2 driver 21ff768f59SMauro Carvalho Chehaband user space. It handles the allocation and management of buffers for 22ff768f59SMauro Carvalho Chehabthe storage of video frames. There is a set of functions which can be used 23ff768f59SMauro Carvalho Chehabto implement many of the standard POSIX I/O system calls, including read(), 24ff768f59SMauro Carvalho Chehabpoll(), and, happily, mmap(). Another set of functions can be used to 25ff768f59SMauro Carvalho Chehabimplement the bulk of the V4L2 ioctl() calls related to streaming I/O, 26ff768f59SMauro Carvalho Chehabincluding buffer allocation, queueing and dequeueing, and streaming 27ff768f59SMauro Carvalho Chehabcontrol. Using videobuf imposes a few design decisions on the driver 28ff768f59SMauro Carvalho Chehabauthor, but the payback comes in the form of reduced code in the driver and 29ff768f59SMauro Carvalho Chehaba consistent implementation of the V4L2 user-space API. 30ff768f59SMauro Carvalho Chehab 31ff768f59SMauro Carvalho ChehabBuffer types 32ff768f59SMauro Carvalho Chehab------------ 33ff768f59SMauro Carvalho Chehab 34ff768f59SMauro Carvalho ChehabNot all video devices use the same kind of buffers. In fact, there are (at 35ff768f59SMauro Carvalho Chehableast) three common variations: 36ff768f59SMauro Carvalho Chehab 37ff768f59SMauro Carvalho Chehab - Buffers which are scattered in both the physical and (kernel) virtual 38ff768f59SMauro Carvalho Chehab address spaces. (Almost) all user-space buffers are like this, but it 39ff768f59SMauro Carvalho Chehab makes great sense to allocate kernel-space buffers this way as well when 40ff768f59SMauro Carvalho Chehab it is possible. Unfortunately, it is not always possible; working with 41ff768f59SMauro Carvalho Chehab this kind of buffer normally requires hardware which can do 42ff768f59SMauro Carvalho Chehab scatter/gather DMA operations. 43ff768f59SMauro Carvalho Chehab 44ff768f59SMauro Carvalho Chehab - Buffers which are physically scattered, but which are virtually 45ff768f59SMauro Carvalho Chehab contiguous; buffers allocated with vmalloc(), in other words. These 46ff768f59SMauro Carvalho Chehab buffers are just as hard to use for DMA operations, but they can be 47ff768f59SMauro Carvalho Chehab useful in situations where DMA is not available but virtually-contiguous 48ff768f59SMauro Carvalho Chehab buffers are convenient. 49ff768f59SMauro Carvalho Chehab 50ff768f59SMauro Carvalho Chehab - Buffers which are physically contiguous. Allocation of this kind of 51ff768f59SMauro Carvalho Chehab buffer can be unreliable on fragmented systems, but simpler DMA 52ff768f59SMauro Carvalho Chehab controllers cannot deal with anything else. 53ff768f59SMauro Carvalho Chehab 54ff768f59SMauro Carvalho ChehabVideobuf can work with all three types of buffers, but the driver author 55ff768f59SMauro Carvalho Chehabmust pick one at the outset and design the driver around that decision. 56ff768f59SMauro Carvalho Chehab 57ff768f59SMauro Carvalho Chehab[It's worth noting that there's a fourth kind of buffer: "overlay" buffers 58ff768f59SMauro Carvalho Chehabwhich are located within the system's video memory. The overlay 59ff768f59SMauro Carvalho Chehabfunctionality is considered to be deprecated for most use, but it still 60ff768f59SMauro Carvalho Chehabshows up occasionally in system-on-chip drivers where the performance 61ff768f59SMauro Carvalho Chehabbenefits merit the use of this technique. Overlay buffers can be handled 62ff768f59SMauro Carvalho Chehabas a form of scattered buffer, but there are very few implementations in 63ff768f59SMauro Carvalho Chehabthe kernel and a description of this technique is currently beyond the 64ff768f59SMauro Carvalho Chehabscope of this document.] 65ff768f59SMauro Carvalho Chehab 66ff768f59SMauro Carvalho ChehabData structures, callbacks, and initialization 67ff768f59SMauro Carvalho Chehab---------------------------------------------- 68ff768f59SMauro Carvalho Chehab 69ff768f59SMauro Carvalho ChehabDepending on which type of buffers are being used, the driver should 70ff768f59SMauro Carvalho Chehabinclude one of the following files: 71ff768f59SMauro Carvalho Chehab 72ff768f59SMauro Carvalho Chehab.. code-block:: none 73ff768f59SMauro Carvalho Chehab 74ff768f59SMauro Carvalho Chehab <media/videobuf-dma-sg.h> /* Physically scattered */ 75ff768f59SMauro Carvalho Chehab <media/videobuf-vmalloc.h> /* vmalloc() buffers */ 76ff768f59SMauro Carvalho Chehab <media/videobuf-dma-contig.h> /* Physically contiguous */ 77ff768f59SMauro Carvalho Chehab 78ff768f59SMauro Carvalho ChehabThe driver's data structure describing a V4L2 device should include a 79ff768f59SMauro Carvalho Chehabstruct videobuf_queue instance for the management of the buffer queue, 80ff768f59SMauro Carvalho Chehabalong with a list_head for the queue of available buffers. There will also 81ff768f59SMauro Carvalho Chehabneed to be an interrupt-safe spinlock which is used to protect (at least) 82ff768f59SMauro Carvalho Chehabthe queue. 83ff768f59SMauro Carvalho Chehab 84ff768f59SMauro Carvalho ChehabThe next step is to write four simple callbacks to help videobuf deal with 85ff768f59SMauro Carvalho Chehabthe management of buffers: 86ff768f59SMauro Carvalho Chehab 87ff768f59SMauro Carvalho Chehab.. code-block:: none 88ff768f59SMauro Carvalho Chehab 89ff768f59SMauro Carvalho Chehab struct videobuf_queue_ops { 90ff768f59SMauro Carvalho Chehab int (*buf_setup)(struct videobuf_queue *q, 91ff768f59SMauro Carvalho Chehab unsigned int *count, unsigned int *size); 92ff768f59SMauro Carvalho Chehab int (*buf_prepare)(struct videobuf_queue *q, 93ff768f59SMauro Carvalho Chehab struct videobuf_buffer *vb, 94ff768f59SMauro Carvalho Chehab enum v4l2_field field); 95ff768f59SMauro Carvalho Chehab void (*buf_queue)(struct videobuf_queue *q, 96ff768f59SMauro Carvalho Chehab struct videobuf_buffer *vb); 97ff768f59SMauro Carvalho Chehab void (*buf_release)(struct videobuf_queue *q, 98ff768f59SMauro Carvalho Chehab struct videobuf_buffer *vb); 99ff768f59SMauro Carvalho Chehab }; 100ff768f59SMauro Carvalho Chehab 101ff768f59SMauro Carvalho Chehabbuf_setup() is called early in the I/O process, when streaming is being 102ff768f59SMauro Carvalho Chehabinitiated; its purpose is to tell videobuf about the I/O stream. The count 103ff768f59SMauro Carvalho Chehabparameter will be a suggested number of buffers to use; the driver should 104ff768f59SMauro Carvalho Chehabcheck it for rationality and adjust it if need be. As a practical rule, a 105ff768f59SMauro Carvalho Chehabminimum of two buffers are needed for proper streaming, and there is 106ff768f59SMauro Carvalho Chehabusually a maximum (which cannot exceed 32) which makes sense for each 107ff768f59SMauro Carvalho Chehabdevice. The size parameter should be set to the expected (maximum) size 108ff768f59SMauro Carvalho Chehabfor each frame of data. 109ff768f59SMauro Carvalho Chehab 110ff768f59SMauro Carvalho ChehabEach buffer (in the form of a struct videobuf_buffer pointer) will be 111ff768f59SMauro Carvalho Chehabpassed to buf_prepare(), which should set the buffer's size, width, height, 112ff768f59SMauro Carvalho Chehaband field fields properly. If the buffer's state field is 113ff768f59SMauro Carvalho ChehabVIDEOBUF_NEEDS_INIT, the driver should pass it to: 114ff768f59SMauro Carvalho Chehab 115ff768f59SMauro Carvalho Chehab.. code-block:: none 116ff768f59SMauro Carvalho Chehab 117ff768f59SMauro Carvalho Chehab int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb, 118ff768f59SMauro Carvalho Chehab struct v4l2_framebuffer *fbuf); 119ff768f59SMauro Carvalho Chehab 120ff768f59SMauro Carvalho ChehabAmong other things, this call will usually allocate memory for the buffer. 121ff768f59SMauro Carvalho ChehabFinally, the buf_prepare() function should set the buffer's state to 122ff768f59SMauro Carvalho ChehabVIDEOBUF_PREPARED. 123ff768f59SMauro Carvalho Chehab 124ff768f59SMauro Carvalho ChehabWhen a buffer is queued for I/O, it is passed to buf_queue(), which should 125ff768f59SMauro Carvalho Chehabput it onto the driver's list of available buffers and set its state to 126ff768f59SMauro Carvalho ChehabVIDEOBUF_QUEUED. Note that this function is called with the queue spinlock 127ff768f59SMauro Carvalho Chehabheld; if it tries to acquire it as well things will come to a screeching 128ff768f59SMauro Carvalho Chehabhalt. Yes, this is the voice of experience. Note also that videobuf may 129ff768f59SMauro Carvalho Chehabwait on the first buffer in the queue; placing other buffers in front of it 130ff768f59SMauro Carvalho Chehabcould again gum up the works. So use list_add_tail() to enqueue buffers. 131ff768f59SMauro Carvalho Chehab 132ff768f59SMauro Carvalho ChehabFinally, buf_release() is called when a buffer is no longer intended to be 133ff768f59SMauro Carvalho Chehabused. The driver should ensure that there is no I/O active on the buffer, 134ff768f59SMauro Carvalho Chehabthen pass it to the appropriate free routine(s): 135ff768f59SMauro Carvalho Chehab 136ff768f59SMauro Carvalho Chehab.. code-block:: none 137ff768f59SMauro Carvalho Chehab 138ff768f59SMauro Carvalho Chehab /* Scatter/gather drivers */ 139ff768f59SMauro Carvalho Chehab int videobuf_dma_unmap(struct videobuf_queue *q, 140ff768f59SMauro Carvalho Chehab struct videobuf_dmabuf *dma); 141ff768f59SMauro Carvalho Chehab int videobuf_dma_free(struct videobuf_dmabuf *dma); 142ff768f59SMauro Carvalho Chehab 143ff768f59SMauro Carvalho Chehab /* vmalloc drivers */ 144ff768f59SMauro Carvalho Chehab void videobuf_vmalloc_free (struct videobuf_buffer *buf); 145ff768f59SMauro Carvalho Chehab 146ff768f59SMauro Carvalho Chehab /* Contiguous drivers */ 147ff768f59SMauro Carvalho Chehab void videobuf_dma_contig_free(struct videobuf_queue *q, 148ff768f59SMauro Carvalho Chehab struct videobuf_buffer *buf); 149ff768f59SMauro Carvalho Chehab 150ff768f59SMauro Carvalho ChehabOne way to ensure that a buffer is no longer under I/O is to pass it to: 151ff768f59SMauro Carvalho Chehab 152ff768f59SMauro Carvalho Chehab.. code-block:: none 153ff768f59SMauro Carvalho Chehab 154ff768f59SMauro Carvalho Chehab int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr); 155ff768f59SMauro Carvalho Chehab 156ff768f59SMauro Carvalho ChehabHere, vb is the buffer, non_blocking indicates whether non-blocking I/O 157ff768f59SMauro Carvalho Chehabshould be used (it should be zero in the buf_release() case), and intr 158ff768f59SMauro Carvalho Chehabcontrols whether an interruptible wait is used. 159ff768f59SMauro Carvalho Chehab 160ff768f59SMauro Carvalho ChehabFile operations 161ff768f59SMauro Carvalho Chehab--------------- 162ff768f59SMauro Carvalho Chehab 163ff768f59SMauro Carvalho ChehabAt this point, much of the work is done; much of the rest is slipping 164ff768f59SMauro Carvalho Chehabvideobuf calls into the implementation of the other driver callbacks. The 165ff768f59SMauro Carvalho Chehabfirst step is in the open() function, which must initialize the 166ff768f59SMauro Carvalho Chehabvideobuf queue. The function to use depends on the type of buffer used: 167ff768f59SMauro Carvalho Chehab 168ff768f59SMauro Carvalho Chehab.. code-block:: none 169ff768f59SMauro Carvalho Chehab 170ff768f59SMauro Carvalho Chehab void videobuf_queue_sg_init(struct videobuf_queue *q, 171ff768f59SMauro Carvalho Chehab struct videobuf_queue_ops *ops, 172ff768f59SMauro Carvalho Chehab struct device *dev, 173ff768f59SMauro Carvalho Chehab spinlock_t *irqlock, 174ff768f59SMauro Carvalho Chehab enum v4l2_buf_type type, 175ff768f59SMauro Carvalho Chehab enum v4l2_field field, 176ff768f59SMauro Carvalho Chehab unsigned int msize, 177ff768f59SMauro Carvalho Chehab void *priv); 178ff768f59SMauro Carvalho Chehab 179ff768f59SMauro Carvalho Chehab void videobuf_queue_vmalloc_init(struct videobuf_queue *q, 180ff768f59SMauro Carvalho Chehab struct videobuf_queue_ops *ops, 181ff768f59SMauro Carvalho Chehab struct device *dev, 182ff768f59SMauro Carvalho Chehab spinlock_t *irqlock, 183ff768f59SMauro Carvalho Chehab enum v4l2_buf_type type, 184ff768f59SMauro Carvalho Chehab enum v4l2_field field, 185ff768f59SMauro Carvalho Chehab unsigned int msize, 186ff768f59SMauro Carvalho Chehab void *priv); 187ff768f59SMauro Carvalho Chehab 188ff768f59SMauro Carvalho Chehab void videobuf_queue_dma_contig_init(struct videobuf_queue *q, 189ff768f59SMauro Carvalho Chehab struct videobuf_queue_ops *ops, 190ff768f59SMauro Carvalho Chehab struct device *dev, 191ff768f59SMauro Carvalho Chehab spinlock_t *irqlock, 192ff768f59SMauro Carvalho Chehab enum v4l2_buf_type type, 193ff768f59SMauro Carvalho Chehab enum v4l2_field field, 194ff768f59SMauro Carvalho Chehab unsigned int msize, 195ff768f59SMauro Carvalho Chehab void *priv); 196ff768f59SMauro Carvalho Chehab 197ff768f59SMauro Carvalho ChehabIn each case, the parameters are the same: q is the queue structure for the 198ff768f59SMauro Carvalho Chehabdevice, ops is the set of callbacks as described above, dev is the device 199ff768f59SMauro Carvalho Chehabstructure for this video device, irqlock is an interrupt-safe spinlock to 200ff768f59SMauro Carvalho Chehabprotect access to the data structures, type is the buffer type used by the 201ff768f59SMauro Carvalho Chehabdevice (cameras will use V4L2_BUF_TYPE_VIDEO_CAPTURE, for example), field 202ff768f59SMauro Carvalho Chehabdescribes which field is being captured (often V4L2_FIELD_NONE for 203ff768f59SMauro Carvalho Chehabprogressive devices), msize is the size of any containing structure used 204ff768f59SMauro Carvalho Chehabaround struct videobuf_buffer, and priv is a private data pointer which 205ff768f59SMauro Carvalho Chehabshows up in the priv_data field of struct videobuf_queue. Note that these 206ff768f59SMauro Carvalho Chehabare void functions which, evidently, are immune to failure. 207ff768f59SMauro Carvalho Chehab 208ff768f59SMauro Carvalho ChehabV4L2 capture drivers can be written to support either of two APIs: the 209ff768f59SMauro Carvalho Chehabread() system call and the rather more complicated streaming mechanism. As 210ff768f59SMauro Carvalho Chehaba general rule, it is necessary to support both to ensure that all 211ff768f59SMauro Carvalho Chehabapplications have a chance of working with the device. Videobuf makes it 212ff768f59SMauro Carvalho Chehabeasy to do that with the same code. To implement read(), the driver need 213ff768f59SMauro Carvalho Chehabonly make a call to one of: 214ff768f59SMauro Carvalho Chehab 215ff768f59SMauro Carvalho Chehab.. code-block:: none 216ff768f59SMauro Carvalho Chehab 217ff768f59SMauro Carvalho Chehab ssize_t videobuf_read_one(struct videobuf_queue *q, 218ff768f59SMauro Carvalho Chehab char __user *data, size_t count, 219ff768f59SMauro Carvalho Chehab loff_t *ppos, int nonblocking); 220ff768f59SMauro Carvalho Chehab 221ff768f59SMauro Carvalho Chehab ssize_t videobuf_read_stream(struct videobuf_queue *q, 222ff768f59SMauro Carvalho Chehab char __user *data, size_t count, 223ff768f59SMauro Carvalho Chehab loff_t *ppos, int vbihack, int nonblocking); 224ff768f59SMauro Carvalho Chehab 225ff768f59SMauro Carvalho ChehabEither one of these functions will read frame data into data, returning the 226ff768f59SMauro Carvalho Chehabamount actually read; the difference is that videobuf_read_one() will only 227ff768f59SMauro Carvalho Chehabread a single frame, while videobuf_read_stream() will read multiple frames 228ff768f59SMauro Carvalho Chehabif they are needed to satisfy the count requested by the application. A 229ff768f59SMauro Carvalho Chehabtypical driver read() implementation will start the capture engine, call 230ff768f59SMauro Carvalho Chehabone of the above functions, then stop the engine before returning (though a 231ff768f59SMauro Carvalho Chehabsmarter implementation might leave the engine running for a little while in 232ff768f59SMauro Carvalho Chehabanticipation of another read() call happening in the near future). 233ff768f59SMauro Carvalho Chehab 234ff768f59SMauro Carvalho ChehabThe poll() function can usually be implemented with a direct call to: 235ff768f59SMauro Carvalho Chehab 236ff768f59SMauro Carvalho Chehab.. code-block:: none 237ff768f59SMauro Carvalho Chehab 238ff768f59SMauro Carvalho Chehab unsigned int videobuf_poll_stream(struct file *file, 239ff768f59SMauro Carvalho Chehab struct videobuf_queue *q, 240ff768f59SMauro Carvalho Chehab poll_table *wait); 241ff768f59SMauro Carvalho Chehab 242ff768f59SMauro Carvalho ChehabNote that the actual wait queue eventually used will be the one associated 243ff768f59SMauro Carvalho Chehabwith the first available buffer. 244ff768f59SMauro Carvalho Chehab 245ff768f59SMauro Carvalho ChehabWhen streaming I/O is done to kernel-space buffers, the driver must support 246ff768f59SMauro Carvalho Chehabthe mmap() system call to enable user space to access the data. In many 247ff768f59SMauro Carvalho ChehabV4L2 drivers, the often-complex mmap() implementation simplifies to a 248ff768f59SMauro Carvalho Chehabsingle call to: 249ff768f59SMauro Carvalho Chehab 250ff768f59SMauro Carvalho Chehab.. code-block:: none 251ff768f59SMauro Carvalho Chehab 252ff768f59SMauro Carvalho Chehab int videobuf_mmap_mapper(struct videobuf_queue *q, 253ff768f59SMauro Carvalho Chehab struct vm_area_struct *vma); 254ff768f59SMauro Carvalho Chehab 255ff768f59SMauro Carvalho ChehabEverything else is handled by the videobuf code. 256ff768f59SMauro Carvalho Chehab 257ff768f59SMauro Carvalho ChehabThe release() function requires two separate videobuf calls: 258ff768f59SMauro Carvalho Chehab 259ff768f59SMauro Carvalho Chehab.. code-block:: none 260ff768f59SMauro Carvalho Chehab 261ff768f59SMauro Carvalho Chehab void videobuf_stop(struct videobuf_queue *q); 262ff768f59SMauro Carvalho Chehab int videobuf_mmap_free(struct videobuf_queue *q); 263ff768f59SMauro Carvalho Chehab 264ff768f59SMauro Carvalho ChehabThe call to videobuf_stop() terminates any I/O in progress - though it is 265ff768f59SMauro Carvalho Chehabstill up to the driver to stop the capture engine. The call to 266ff768f59SMauro Carvalho Chehabvideobuf_mmap_free() will ensure that all buffers have been unmapped; if 267ff768f59SMauro Carvalho Chehabso, they will all be passed to the buf_release() callback. If buffers 268ff768f59SMauro Carvalho Chehabremain mapped, videobuf_mmap_free() returns an error code instead. The 269ff768f59SMauro Carvalho Chehabpurpose is clearly to cause the closing of the file descriptor to fail if 270ff768f59SMauro Carvalho Chehabbuffers are still mapped, but every driver in the 2.6.32 kernel cheerfully 271ff768f59SMauro Carvalho Chehabignores its return value. 272ff768f59SMauro Carvalho Chehab 273ff768f59SMauro Carvalho Chehabioctl() operations 274ff768f59SMauro Carvalho Chehab------------------ 275ff768f59SMauro Carvalho Chehab 276ff768f59SMauro Carvalho ChehabThe V4L2 API includes a very long list of driver callbacks to respond to 277ff768f59SMauro Carvalho Chehabthe many ioctl() commands made available to user space. A number of these 278ff768f59SMauro Carvalho Chehab- those associated with streaming I/O - turn almost directly into videobuf 279ff768f59SMauro Carvalho Chehabcalls. The relevant helper functions are: 280ff768f59SMauro Carvalho Chehab 281ff768f59SMauro Carvalho Chehab.. code-block:: none 282ff768f59SMauro Carvalho Chehab 283ff768f59SMauro Carvalho Chehab int videobuf_reqbufs(struct videobuf_queue *q, 284ff768f59SMauro Carvalho Chehab struct v4l2_requestbuffers *req); 285ff768f59SMauro Carvalho Chehab int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); 286ff768f59SMauro Carvalho Chehab int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b); 287ff768f59SMauro Carvalho Chehab int videobuf_dqbuf(struct videobuf_queue *q, struct v4l2_buffer *b, 288ff768f59SMauro Carvalho Chehab int nonblocking); 289ff768f59SMauro Carvalho Chehab int videobuf_streamon(struct videobuf_queue *q); 290ff768f59SMauro Carvalho Chehab int videobuf_streamoff(struct videobuf_queue *q); 291ff768f59SMauro Carvalho Chehab 292ff768f59SMauro Carvalho ChehabSo, for example, a VIDIOC_REQBUFS call turns into a call to the driver's 293ff768f59SMauro Carvalho Chehabvidioc_reqbufs() callback which, in turn, usually only needs to locate the 294ff768f59SMauro Carvalho Chehabproper struct videobuf_queue pointer and pass it to videobuf_reqbufs(). 295ff768f59SMauro Carvalho ChehabThese support functions can replace a great deal of buffer management 296ff768f59SMauro Carvalho Chehabboilerplate in a lot of V4L2 drivers. 297ff768f59SMauro Carvalho Chehab 298ff768f59SMauro Carvalho ChehabThe vidioc_streamon() and vidioc_streamoff() functions will be a bit more 299ff768f59SMauro Carvalho Chehabcomplex, of course, since they will also need to deal with starting and 300ff768f59SMauro Carvalho Chehabstopping the capture engine. 301ff768f59SMauro Carvalho Chehab 302ff768f59SMauro Carvalho ChehabBuffer allocation 303ff768f59SMauro Carvalho Chehab----------------- 304ff768f59SMauro Carvalho Chehab 305ff768f59SMauro Carvalho ChehabThus far, we have talked about buffers, but have not looked at how they are 306ff768f59SMauro Carvalho Chehaballocated. The scatter/gather case is the most complex on this front. For 307ff768f59SMauro Carvalho Chehaballocation, the driver can leave buffer allocation entirely up to the 308ff768f59SMauro Carvalho Chehabvideobuf layer; in this case, buffers will be allocated as anonymous 309ff768f59SMauro Carvalho Chehabuser-space pages and will be very scattered indeed. If the application is 310ff768f59SMauro Carvalho Chehabusing user-space buffers, no allocation is needed; the videobuf layer will 311ff768f59SMauro Carvalho Chehabtake care of calling get_user_pages() and filling in the scatterlist array. 312ff768f59SMauro Carvalho Chehab 313ff768f59SMauro Carvalho ChehabIf the driver needs to do its own memory allocation, it should be done in 314ff768f59SMauro Carvalho Chehabthe vidioc_reqbufs() function, *after* calling videobuf_reqbufs(). The 315ff768f59SMauro Carvalho Chehabfirst step is a call to: 316ff768f59SMauro Carvalho Chehab 317ff768f59SMauro Carvalho Chehab.. code-block:: none 318ff768f59SMauro Carvalho Chehab 319ff768f59SMauro Carvalho Chehab struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf); 320ff768f59SMauro Carvalho Chehab 321ff768f59SMauro Carvalho ChehabThe returned videobuf_dmabuf structure (defined in 322ff768f59SMauro Carvalho Chehab<media/videobuf-dma-sg.h>) includes a couple of relevant fields: 323ff768f59SMauro Carvalho Chehab 324ff768f59SMauro Carvalho Chehab.. code-block:: none 325ff768f59SMauro Carvalho Chehab 326ff768f59SMauro Carvalho Chehab struct scatterlist *sglist; 327ff768f59SMauro Carvalho Chehab int sglen; 328ff768f59SMauro Carvalho Chehab 329ff768f59SMauro Carvalho ChehabThe driver must allocate an appropriately-sized scatterlist array and 330ff768f59SMauro Carvalho Chehabpopulate it with pointers to the pieces of the allocated buffer; sglen 331ff768f59SMauro Carvalho Chehabshould be set to the length of the array. 332ff768f59SMauro Carvalho Chehab 333ff768f59SMauro Carvalho ChehabDrivers using the vmalloc() method need not (and cannot) concern themselves 334ff768f59SMauro Carvalho Chehabwith buffer allocation at all; videobuf will handle those details. The 335ff768f59SMauro Carvalho Chehabsame is normally true of contiguous-DMA drivers as well; videobuf will 336ff768f59SMauro Carvalho Chehaballocate the buffers (with dma_alloc_coherent()) when it sees fit. That 337ff768f59SMauro Carvalho Chehabmeans that these drivers may be trying to do high-order allocations at any 338ff768f59SMauro Carvalho Chehabtime, an operation which is not always guaranteed to work. Some drivers 339ff768f59SMauro Carvalho Chehabplay tricks by allocating DMA space at system boot time; videobuf does not 340ff768f59SMauro Carvalho Chehabcurrently play well with those drivers. 341ff768f59SMauro Carvalho Chehab 342ff768f59SMauro Carvalho ChehabAs of 2.6.31, contiguous-DMA drivers can work with a user-supplied buffer, 343ff768f59SMauro Carvalho Chehabas long as that buffer is physically contiguous. Normal user-space 344ff768f59SMauro Carvalho Chehaballocations will not meet that criterion, but buffers obtained from other 345ff768f59SMauro Carvalho Chehabkernel drivers, or those contained within huge pages, will work with these 346ff768f59SMauro Carvalho Chehabdrivers. 347ff768f59SMauro Carvalho Chehab 348ff768f59SMauro Carvalho ChehabFilling the buffers 349ff768f59SMauro Carvalho Chehab------------------- 350ff768f59SMauro Carvalho Chehab 351ff768f59SMauro Carvalho ChehabThe final part of a videobuf implementation has no direct callback - it's 352ff768f59SMauro Carvalho Chehabthe portion of the code which actually puts frame data into the buffers, 353ff768f59SMauro Carvalho Chehabusually in response to interrupts from the device. For all types of 354ff768f59SMauro Carvalho Chehabdrivers, this process works approximately as follows: 355ff768f59SMauro Carvalho Chehab 356ff768f59SMauro Carvalho Chehab - Obtain the next available buffer and make sure that somebody is actually 357ff768f59SMauro Carvalho Chehab waiting for it. 358ff768f59SMauro Carvalho Chehab 359ff768f59SMauro Carvalho Chehab - Get a pointer to the memory and put video data there. 360ff768f59SMauro Carvalho Chehab 361ff768f59SMauro Carvalho Chehab - Mark the buffer as done and wake up the process waiting for it. 362ff768f59SMauro Carvalho Chehab 363ff768f59SMauro Carvalho ChehabStep (1) above is done by looking at the driver-managed list_head structure 364ff768f59SMauro Carvalho Chehab- the one which is filled in the buf_queue() callback. Because starting 365ff768f59SMauro Carvalho Chehabthe engine and enqueueing buffers are done in separate steps, it's possible 366ff768f59SMauro Carvalho Chehabfor the engine to be running without any buffers available - in the 367ff768f59SMauro Carvalho Chehabvmalloc() case especially. So the driver should be prepared for the list 368ff768f59SMauro Carvalho Chehabto be empty. It is equally possible that nobody is yet interested in the 369ff768f59SMauro Carvalho Chehabbuffer; the driver should not remove it from the list or fill it until a 370ff768f59SMauro Carvalho Chehabprocess is waiting on it. That test can be done by examining the buffer's 371ff768f59SMauro Carvalho Chehabdone field (a wait_queue_head_t structure) with waitqueue_active(). 372ff768f59SMauro Carvalho Chehab 373ff768f59SMauro Carvalho ChehabA buffer's state should be set to VIDEOBUF_ACTIVE before being mapped for 374ff768f59SMauro Carvalho ChehabDMA; that ensures that the videobuf layer will not try to do anything with 375ff768f59SMauro Carvalho Chehabit while the device is transferring data. 376ff768f59SMauro Carvalho Chehab 377ff768f59SMauro Carvalho ChehabFor scatter/gather drivers, the needed memory pointers will be found in the 378ff768f59SMauro Carvalho Chehabscatterlist structure described above. Drivers using the vmalloc() method 379ff768f59SMauro Carvalho Chehabcan get a memory pointer with: 380ff768f59SMauro Carvalho Chehab 381ff768f59SMauro Carvalho Chehab.. code-block:: none 382ff768f59SMauro Carvalho Chehab 383ff768f59SMauro Carvalho Chehab void *videobuf_to_vmalloc(struct videobuf_buffer *buf); 384ff768f59SMauro Carvalho Chehab 385ff768f59SMauro Carvalho ChehabFor contiguous DMA drivers, the function to use is: 386ff768f59SMauro Carvalho Chehab 387ff768f59SMauro Carvalho Chehab.. code-block:: none 388ff768f59SMauro Carvalho Chehab 389ff768f59SMauro Carvalho Chehab dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf); 390ff768f59SMauro Carvalho Chehab 391ff768f59SMauro Carvalho ChehabThe contiguous DMA API goes out of its way to hide the kernel-space address 392ff768f59SMauro Carvalho Chehabof the DMA buffer from drivers. 393ff768f59SMauro Carvalho Chehab 394ff768f59SMauro Carvalho ChehabThe final step is to set the size field of the relevant videobuf_buffer 395ff768f59SMauro Carvalho Chehabstructure to the actual size of the captured image, set state to 396ff768f59SMauro Carvalho ChehabVIDEOBUF_DONE, then call wake_up() on the done queue. At this point, the 397ff768f59SMauro Carvalho Chehabbuffer is owned by the videobuf layer and the driver should not touch it 398ff768f59SMauro Carvalho Chehabagain. 399ff768f59SMauro Carvalho Chehab 400ff768f59SMauro Carvalho ChehabDevelopers who are interested in more information can go into the relevant 401ff768f59SMauro Carvalho Chehabheader files; there are a few low-level functions declared there which have 402*143f8adfSHans Verkuilnot been talked about here. Note also that all of these calls are exported 403ff768f59SMauro Carvalho ChehabGPL-only, so they will not be available to non-GPL kernel modules. 404