v4l2-videobuf.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _vb_framework:
  3. Videobuf Framework
  4. ==================
  5. Author: Jonathan Corbet <corbet@lwn.net>
  6. Current as of 2.6.33
  7. .. note::
  8. The videobuf framework was deprecated in favor of videobuf2. Shouldn't
  9. be used on new drivers.
  10. Introduction
  11. ------------
  12. The videobuf layer functions as a sort of glue layer between a V4L2 driver
  13. and user space. It handles the allocation and management of buffers for
  14. the storage of video frames. There is a set of functions which can be used
  15. to implement many of the standard POSIX I/O system calls, including read(),
  16. poll(), and, happily, mmap(). Another set of functions can be used to
  17. implement the bulk of the V4L2 ioctl() calls related to streaming I/O,
  18. including buffer allocation, queueing and dequeueing, and streaming
  19. control. Using videobuf imposes a few design decisions on the driver
  20. author, but the payback comes in the form of reduced code in the driver and
  21. a consistent implementation of the V4L2 user-space API.
  22. Buffer types
  23. ------------
  24. Not all video devices use the same kind of buffers. In fact, there are (at
  25. least) three common variations:
  26. - Buffers which are scattered in both the physical and (kernel) virtual
  27. address spaces. (Almost) all user-space buffers are like this, but it
  28. makes great sense to allocate kernel-space buffers this way as well when
  29. it is possible. Unfortunately, it is not always possible; working with
  30. this kind of buffer normally requires hardware which can do
  31. scatter/gather DMA operations.
  32. - Buffers which are physically scattered, but which are virtually
  33. contiguous; buffers allocated with vmalloc(), in other words. These
  34. buffers are just as hard to use for DMA operations, but they can be
  35. useful in situations where DMA is not available but virtually-contiguous
  36. buffers are convenient.
  37. - Buffers which are physically contiguous. Allocation of this kind of
  38. buffer can be unreliable on fragmented systems, but simpler DMA
  39. controllers cannot deal with anything else.
  40. Videobuf can work with all three types of buffers, but the driver author
  41. must pick one at the outset and design the driver around that decision.
  42. [It's worth noting that there's a fourth kind of buffer: "overlay" buffers
  43. which are located within the system's video memory. The overlay
  44. functionality is considered to be deprecated for most use, but it still
  45. shows up occasionally in system-on-chip drivers where the performance
  46. benefits merit the use of this technique. Overlay buffers can be handled
  47. as a form of scattered buffer, but there are very few implementations in
  48. the kernel and a description of this technique is currently beyond the
  49. scope of this document.]
  50. Data structures, callbacks, and initialization
  51. ----------------------------------------------
  52. Depending on which type of buffers are being used, the driver should
  53. include one of the following files:
  54. .. code-block:: none
  55. <media/videobuf-dma-sg.h> /* Physically scattered */
  56. <media/videobuf-vmalloc.h> /* vmalloc() buffers */
  57. <media/videobuf-dma-contig.h> /* Physically contiguous */
  58. The driver's data structure describing a V4L2 device should include a
  59. struct videobuf_queue instance for the management of the buffer queue,
  60. along with a list_head for the queue of available buffers. There will also
  61. need to be an interrupt-safe spinlock which is used to protect (at least)
  62. the queue.
  63. The next step is to write four simple callbacks to help videobuf deal with
  64. the management of buffers:
  65. .. code-block:: none
  66. struct videobuf_queue_ops {
  67. int (*buf_setup)(struct videobuf_queue *q,
  68. unsigned int *count, unsigned int *size);
  69. int (*buf_prepare)(struct videobuf_queue *q,
  70. struct videobuf_buffer *vb,
  71. enum v4l2_field field);
  72. void (*buf_queue)(struct videobuf_queue *q,
  73. struct videobuf_buffer *vb);
  74. void (*buf_release)(struct videobuf_queue *q,
  75. struct videobuf_buffer *vb);
  76. };
  77. buf_setup() is called early in the I/O process, when streaming is being
  78. initiated; its purpose is to tell videobuf about the I/O stream. The count
  79. parameter will be a suggested number of buffers to use; the driver should
  80. check it for rationality and adjust it if need be. As a practical rule, a
  81. minimum of two buffers are needed for proper streaming, and there is
  82. usually a maximum (which cannot exceed 32) which makes sense for each
  83. device. The size parameter should be set to the expected (maximum) size
  84. for each frame of data.
  85. Each buffer (in the form of a struct videobuf_buffer pointer) will be
  86. passed to buf_prepare(), which should set the buffer's size, width, height,
  87. and field fields properly. If the buffer's state field is
  88. VIDEOBUF_NEEDS_INIT, the driver should pass it to:
  89. .. code-block:: none
  90. int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
  91. struct v4l2_framebuffer *fbuf);
  92. Among other things, this call will usually allocate memory for the buffer.
  93. Finally, the buf_prepare() function should set the buffer's state to
  94. VIDEOBUF_PREPARED.
  95. When a buffer is queued for I/O, it is passed to buf_queue(), which should
  96. put it onto the driver's list of available buffers and set its state to
  97. VIDEOBUF_QUEUED. Note that this function is called with the queue spinlock
  98. held; if it tries to acquire it as well things will come to a screeching
  99. halt. Yes, this is the voice of experience. Note also that videobuf may
  100. wait on the first buffer in the queue; placing other buffers in front of it
  101. could again gum up the works. So use list_add_tail() to enqueue buffers.
  102. Finally, buf_release() is called when a buffer is no longer intended to be
  103. used. The driver should ensure that there is no I/O active on the buffer,
  104. then pass it to the appropriate free routine(s):
  105. .. code-block:: none
  106. /* Scatter/gather drivers */
  107. int videobuf_dma_unmap(struct videobuf_queue *q,
  108. struct videobuf_dmabuf *dma);
  109. int videobuf_dma_free(struct videobuf_dmabuf *dma);
  110. /* vmalloc drivers */
  111. void videobuf_vmalloc_free (struct videobuf_buffer *buf);
  112. /* Contiguous drivers */
  113. void videobuf_dma_contig_free(struct videobuf_queue *q,
  114. struct videobuf_buffer *buf);
  115. One way to ensure that a buffer is no longer under I/O is to pass it to:
  116. .. code-block:: none
  117. int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
  118. Here, vb is the buffer, non_blocking indicates whether non-blocking I/O
  119. should be used (it should be zero in the buf_release() case), and intr
  120. controls whether an interruptible wait is used.
  121. File operations
  122. ---------------
  123. At this point, much of the work is done; much of the rest is slipping
  124. videobuf calls into the implementation of the other driver callbacks. The
  125. first step is in the open() function, which must initialize the
  126. videobuf queue. The function to use depends on the type of buffer used:
  127. .. code-block:: none
  128. void videobuf_queue_sg_init(struct videobuf_queue *q,
  129. struct videobuf_queue_ops *ops,
  130. struct device *dev,
  131. spinlock_t *irqlock,
  132. enum v4l2_buf_type type,
  133. enum v4l2_field field,
  134. unsigned int msize,
  135. void *priv);
  136. void videobuf_queue_vmalloc_init(struct videobuf_queue *q,
  137. struct videobuf_queue_ops *ops,
  138. struct device *dev,
  139. spinlock_t *irqlock,
  140. enum v4l2_buf_type type,
  141. enum v4l2_field field,
  142. unsigned int msize,
  143. void *priv);
  144. void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
  145. struct videobuf_queue_ops *ops,
  146. struct device *dev,
  147. spinlock_t *irqlock,
  148. enum v4l2_buf_type type,
  149. enum v4l2_field field,
  150. unsigned int msize,
  151. void *priv);
  152. In each case, the parameters are the same: q is the queue structure for the
  153. device, ops is the set of callbacks as described above, dev is the device
  154. structure for this video device, irqlock is an interrupt-safe spinlock to
  155. protect access to the data structures, type is the buffer type used by the
  156. device (cameras will use V4L2_BUF_TYPE_VIDEO_CAPTURE, for example), field
  157. describes which field is being captured (often V4L2_FIELD_NONE for
  158. progressive devices), msize is the size of any containing structure used
  159. around struct videobuf_buffer, and priv is a private data pointer which
  160. shows up in the priv_data field of struct videobuf_queue. Note that these
  161. are void functions which, evidently, are immune to failure.
  162. V4L2 capture drivers can be written to support either of two APIs: the
  163. read() system call and the rather more complicated streaming mechanism. As
  164. a general rule, it is necessary to support both to ensure that all
  165. applications have a chance of working with the device. Videobuf makes it
  166. easy to do that with the same code. To implement read(), the driver need
  167. only make a call to one of:
  168. .. code-block:: none
  169. ssize_t videobuf_read_one(struct videobuf_queue *q,
  170. char __user *data, size_t count,
  171. loff_t *ppos, int nonblocking);
  172. ssize_t videobuf_read_stream(struct videobuf_queue *q,
  173. char __user *data, size_t count,
  174. loff_t *ppos, int vbihack, int nonblocking);
  175. Either one of these functions will read frame data into data, returning the
  176. amount actually read; the difference is that videobuf_read_one() will only
  177. read a single frame, while videobuf_read_stream() will read multiple frames
  178. if they are needed to satisfy the count requested by the application. A
  179. typical driver read() implementation will start the capture engine, call
  180. one of the above functions, then stop the engine before returning (though a
  181. smarter implementation might leave the engine running for a little while in
  182. anticipation of another read() call happening in the near future).
  183. The poll() function can usually be implemented with a direct call to:
  184. .. code-block:: none
  185. unsigned int videobuf_poll_stream(struct file *file,
  186. struct videobuf_queue *q,
  187. poll_table *wait);
  188. Note that the actual wait queue eventually used will be the one associated
  189. with the first available buffer.
  190. When streaming I/O is done to kernel-space buffers, the driver must support
  191. the mmap() system call to enable user space to access the data. In many
  192. V4L2 drivers, the often-complex mmap() implementation simplifies to a
  193. single call to:
  194. .. code-block:: none
  195. int videobuf_mmap_mapper(struct videobuf_queue *q,
  196. struct vm_area_struct *vma);
  197. Everything else is handled by the videobuf code.
  198. The release() function requires two separate videobuf calls:
  199. .. code-block:: none
  200. void videobuf_stop(struct videobuf_queue *q);
  201. int videobuf_mmap_free(struct videobuf_queue *q);
  202. The call to videobuf_stop() terminates any I/O in progress - though it is
  203. still up to the driver to stop the capture engine. The call to
  204. videobuf_mmap_free() will ensure that all buffers have been unmapped; if
  205. so, they will all be passed to the buf_release() callback. If buffers
  206. remain mapped, videobuf_mmap_free() returns an error code instead. The
  207. purpose is clearly to cause the closing of the file descriptor to fail if
  208. buffers are still mapped, but every driver in the 2.6.32 kernel cheerfully
  209. ignores its return value.
  210. ioctl() operations
  211. ------------------
  212. The V4L2 API includes a very long list of driver callbacks to respond to
  213. the many ioctl() commands made available to user space. A number of these
  214. - those associated with streaming I/O - turn almost directly into videobuf
  215. calls. The relevant helper functions are:
  216. .. code-block:: none
  217. int videobuf_reqbufs(struct videobuf_queue *q,
  218. struct v4l2_requestbuffers *req);
  219. int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  220. int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b);
  221. int videobuf_dqbuf(struct videobuf_queue *q, struct v4l2_buffer *b,
  222. int nonblocking);
  223. int videobuf_streamon(struct videobuf_queue *q);
  224. int videobuf_streamoff(struct videobuf_queue *q);
  225. So, for example, a VIDIOC_REQBUFS call turns into a call to the driver's
  226. vidioc_reqbufs() callback which, in turn, usually only needs to locate the
  227. proper struct videobuf_queue pointer and pass it to videobuf_reqbufs().
  228. These support functions can replace a great deal of buffer management
  229. boilerplate in a lot of V4L2 drivers.
  230. The vidioc_streamon() and vidioc_streamoff() functions will be a bit more
  231. complex, of course, since they will also need to deal with starting and
  232. stopping the capture engine.
  233. Buffer allocation
  234. -----------------
  235. Thus far, we have talked about buffers, but have not looked at how they are
  236. allocated. The scatter/gather case is the most complex on this front. For
  237. allocation, the driver can leave buffer allocation entirely up to the
  238. videobuf layer; in this case, buffers will be allocated as anonymous
  239. user-space pages and will be very scattered indeed. If the application is
  240. using user-space buffers, no allocation is needed; the videobuf layer will
  241. take care of calling get_user_pages() and filling in the scatterlist array.
  242. If the driver needs to do its own memory allocation, it should be done in
  243. the vidioc_reqbufs() function, *after* calling videobuf_reqbufs(). The
  244. first step is a call to:
  245. .. code-block:: none
  246. struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf);
  247. The returned videobuf_dmabuf structure (defined in
  248. <media/videobuf-dma-sg.h>) includes a couple of relevant fields:
  249. .. code-block:: none
  250. struct scatterlist *sglist;
  251. int sglen;
  252. The driver must allocate an appropriately-sized scatterlist array and
  253. populate it with pointers to the pieces of the allocated buffer; sglen
  254. should be set to the length of the array.
  255. Drivers using the vmalloc() method need not (and cannot) concern themselves
  256. with buffer allocation at all; videobuf will handle those details. The
  257. same is normally true of contiguous-DMA drivers as well; videobuf will
  258. allocate the buffers (with dma_alloc_coherent()) when it sees fit. That
  259. means that these drivers may be trying to do high-order allocations at any
  260. time, an operation which is not always guaranteed to work. Some drivers
  261. play tricks by allocating DMA space at system boot time; videobuf does not
  262. currently play well with those drivers.
  263. As of 2.6.31, contiguous-DMA drivers can work with a user-supplied buffer,
  264. as long as that buffer is physically contiguous. Normal user-space
  265. allocations will not meet that criterion, but buffers obtained from other
  266. kernel drivers, or those contained within huge pages, will work with these
  267. drivers.
  268. Filling the buffers
  269. -------------------
  270. The final part of a videobuf implementation has no direct callback - it's
  271. the portion of the code which actually puts frame data into the buffers,
  272. usually in response to interrupts from the device. For all types of
  273. drivers, this process works approximately as follows:
  274. - Obtain the next available buffer and make sure that somebody is actually
  275. waiting for it.
  276. - Get a pointer to the memory and put video data there.
  277. - Mark the buffer as done and wake up the process waiting for it.
  278. Step (1) above is done by looking at the driver-managed list_head structure
  279. - the one which is filled in the buf_queue() callback. Because starting
  280. the engine and enqueueing buffers are done in separate steps, it's possible
  281. for the engine to be running without any buffers available - in the
  282. vmalloc() case especially. So the driver should be prepared for the list
  283. to be empty. It is equally possible that nobody is yet interested in the
  284. buffer; the driver should not remove it from the list or fill it until a
  285. process is waiting on it. That test can be done by examining the buffer's
  286. done field (a wait_queue_head_t structure) with waitqueue_active().
  287. A buffer's state should be set to VIDEOBUF_ACTIVE before being mapped for
  288. DMA; that ensures that the videobuf layer will not try to do anything with
  289. it while the device is transferring data.
  290. For scatter/gather drivers, the needed memory pointers will be found in the
  291. scatterlist structure described above. Drivers using the vmalloc() method
  292. can get a memory pointer with:
  293. .. code-block:: none
  294. void *videobuf_to_vmalloc(struct videobuf_buffer *buf);
  295. For contiguous DMA drivers, the function to use is:
  296. .. code-block:: none
  297. dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf);
  298. The contiguous DMA API goes out of its way to hide the kernel-space address
  299. of the DMA buffer from drivers.
  300. The final step is to set the size field of the relevant videobuf_buffer
  301. structure to the actual size of the captured image, set state to
  302. VIDEOBUF_DONE, then call wake_up() on the done queue. At this point, the
  303. buffer is owned by the videobuf layer and the driver should not touch it
  304. again.
  305. Developers who are interested in more information can go into the relevant
  306. header files; there are a few low-level functions declared there which have
  307. not been talked about here. Note also that all of these calls are exported
  308. GPL-only, so they will not be available to non-GPL kernel modules.