Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: remove the old videobuf framework

The last driver that still used this old framework has been converted
to the videobuf2 framework. So it is now time to delete the old videobuf
code.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>

-3460
-1
Documentation/driver-api/media/v4l2-core.rst
··· 13 13 v4l2-subdev 14 14 v4l2-event 15 15 v4l2-controls 16 - v4l2-videobuf 17 16 v4l2-videobuf2 18 17 v4l2-dv-timings 19 18 v4l2-flash-led-class
-8
Documentation/driver-api/media/v4l2-dev.rst
··· 157 157 Of course, you can always do all the locking yourself by leaving both lock 158 158 pointers at ``NULL``. 159 159 160 - If you use the old :ref:`videobuf framework <vb_framework>` then you must 161 - pass the :c:type:`video_device`->lock to the videobuf queue initialize 162 - function: if videobuf has to wait for a frame to arrive, then it will 163 - temporarily unlock the lock and relock it afterwards. If your driver also 164 - waits in the code, then you should do the same to allow other 165 - processes to access the device node while the first process is waiting for 166 - something. 167 - 168 160 In the case of :ref:`videobuf2 <vb2_framework>` you will need to implement the 169 161 ``wait_prepare()`` and ``wait_finish()`` callbacks to unlock/lock if applicable. 170 162 If you use the ``queue->lock`` pointer, then you can use the helper functions
-403
Documentation/driver-api/media/v4l2-videobuf.rst
··· 1 - .. SPDX-License-Identifier: GPL-2.0 2 - 3 - .. _vb_framework: 4 - 5 - Videobuf Framework 6 - ================== 7 - 8 - Author: Jonathan Corbet <corbet@lwn.net> 9 - 10 - Current as of 2.6.33 11 - 12 - .. note:: 13 - 14 - The videobuf framework was deprecated in favor of videobuf2. Shouldn't 15 - be used on new drivers. 16 - 17 - Introduction 18 - ------------ 19 - 20 - The videobuf layer functions as a sort of glue layer between a V4L2 driver 21 - and user space. It handles the allocation and management of buffers for 22 - the storage of video frames. There is a set of functions which can be used 23 - to implement many of the standard POSIX I/O system calls, including read(), 24 - poll(), and, happily, mmap(). Another set of functions can be used to 25 - implement the bulk of the V4L2 ioctl() calls related to streaming I/O, 26 - including buffer allocation, queueing and dequeueing, and streaming 27 - control. Using videobuf imposes a few design decisions on the driver 28 - author, but the payback comes in the form of reduced code in the driver and 29 - a consistent implementation of the V4L2 user-space API. 30 - 31 - Buffer types 32 - ------------ 33 - 34 - Not all video devices use the same kind of buffers. In fact, there are (at 35 - least) three common variations: 36 - 37 - - Buffers which are scattered in both the physical and (kernel) virtual 38 - address spaces. (Almost) all user-space buffers are like this, but it 39 - makes great sense to allocate kernel-space buffers this way as well when 40 - it is possible. Unfortunately, it is not always possible; working with 41 - this kind of buffer normally requires hardware which can do 42 - scatter/gather DMA operations. 43 - 44 - - Buffers which are physically scattered, but which are virtually 45 - contiguous; buffers allocated with vmalloc(), in other words. These 46 - buffers are just as hard to use for DMA operations, but they can be 47 - useful in situations where DMA is not available but virtually-contiguous 48 - buffers are convenient. 49 - 50 - - Buffers which are physically contiguous. Allocation of this kind of 51 - buffer can be unreliable on fragmented systems, but simpler DMA 52 - controllers cannot deal with anything else. 53 - 54 - Videobuf can work with all three types of buffers, but the driver author 55 - must pick one at the outset and design the driver around that decision. 56 - 57 - [It's worth noting that there's a fourth kind of buffer: "overlay" buffers 58 - which are located within the system's video memory. The overlay 59 - functionality is considered to be deprecated for most use, but it still 60 - shows up occasionally in system-on-chip drivers where the performance 61 - benefits merit the use of this technique. Overlay buffers can be handled 62 - as a form of scattered buffer, but there are very few implementations in 63 - the kernel and a description of this technique is currently beyond the 64 - scope of this document.] 65 - 66 - Data structures, callbacks, and initialization 67 - ---------------------------------------------- 68 - 69 - Depending on which type of buffers are being used, the driver should 70 - include one of the following files: 71 - 72 - .. code-block:: none 73 - 74 - <media/videobuf-dma-sg.h> /* Physically scattered */ 75 - <media/videobuf-vmalloc.h> /* vmalloc() buffers */ 76 - <media/videobuf-dma-contig.h> /* Physically contiguous */ 77 - 78 - The driver's data structure describing a V4L2 device should include a 79 - struct videobuf_queue instance for the management of the buffer queue, 80 - along with a list_head for the queue of available buffers. There will also 81 - need to be an interrupt-safe spinlock which is used to protect (at least) 82 - the queue. 83 - 84 - The next step is to write four simple callbacks to help videobuf deal with 85 - the management of buffers: 86 - 87 - .. code-block:: none 88 - 89 - struct videobuf_queue_ops { 90 - int (*buf_setup)(struct videobuf_queue *q, 91 - unsigned int *count, unsigned int *size); 92 - int (*buf_prepare)(struct videobuf_queue *q, 93 - struct videobuf_buffer *vb, 94 - enum v4l2_field field); 95 - void (*buf_queue)(struct videobuf_queue *q, 96 - struct videobuf_buffer *vb); 97 - void (*buf_release)(struct videobuf_queue *q, 98 - struct videobuf_buffer *vb); 99 - }; 100 - 101 - buf_setup() is called early in the I/O process, when streaming is being 102 - initiated; its purpose is to tell videobuf about the I/O stream. The count 103 - parameter will be a suggested number of buffers to use; the driver should 104 - check it for rationality and adjust it if need be. As a practical rule, a 105 - minimum of two buffers are needed for proper streaming, and there is 106 - usually a maximum (which cannot exceed 32) which makes sense for each 107 - device. The size parameter should be set to the expected (maximum) size 108 - for each frame of data. 109 - 110 - Each buffer (in the form of a struct videobuf_buffer pointer) will be 111 - passed to buf_prepare(), which should set the buffer's size, width, height, 112 - and field fields properly. If the buffer's state field is 113 - VIDEOBUF_NEEDS_INIT, the driver should pass it to: 114 - 115 - .. code-block:: none 116 - 117 - int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb, 118 - struct v4l2_framebuffer *fbuf); 119 - 120 - Among other things, this call will usually allocate memory for the buffer. 121 - Finally, the buf_prepare() function should set the buffer's state to 122 - VIDEOBUF_PREPARED. 123 - 124 - When a buffer is queued for I/O, it is passed to buf_queue(), which should 125 - put it onto the driver's list of available buffers and set its state to 126 - VIDEOBUF_QUEUED. Note that this function is called with the queue spinlock 127 - held; if it tries to acquire it as well things will come to a screeching 128 - halt. Yes, this is the voice of experience. Note also that videobuf may 129 - wait on the first buffer in the queue; placing other buffers in front of it 130 - could again gum up the works. So use list_add_tail() to enqueue buffers. 131 - 132 - Finally, buf_release() is called when a buffer is no longer intended to be 133 - used. The driver should ensure that there is no I/O active on the buffer, 134 - then pass it to the appropriate free routine(s): 135 - 136 - .. code-block:: none 137 - 138 - /* Scatter/gather drivers */ 139 - int videobuf_dma_unmap(struct videobuf_queue *q, 140 - struct videobuf_dmabuf *dma); 141 - int videobuf_dma_free(struct videobuf_dmabuf *dma); 142 - 143 - /* vmalloc drivers */ 144 - void videobuf_vmalloc_free (struct videobuf_buffer *buf); 145 - 146 - /* Contiguous drivers */ 147 - void videobuf_dma_contig_free(struct videobuf_queue *q, 148 - struct videobuf_buffer *buf); 149 - 150 - One way to ensure that a buffer is no longer under I/O is to pass it to: 151 - 152 - .. code-block:: none 153 - 154 - int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr); 155 - 156 - Here, vb is the buffer, non_blocking indicates whether non-blocking I/O 157 - should be used (it should be zero in the buf_release() case), and intr 158 - controls whether an interruptible wait is used. 159 - 160 - File operations 161 - --------------- 162 - 163 - At this point, much of the work is done; much of the rest is slipping 164 - videobuf calls into the implementation of the other driver callbacks. The 165 - first step is in the open() function, which must initialize the 166 - videobuf queue. The function to use depends on the type of buffer used: 167 - 168 - .. code-block:: none 169 - 170 - void videobuf_queue_sg_init(struct videobuf_queue *q, 171 - struct videobuf_queue_ops *ops, 172 - struct device *dev, 173 - spinlock_t *irqlock, 174 - enum v4l2_buf_type type, 175 - enum v4l2_field field, 176 - unsigned int msize, 177 - void *priv); 178 - 179 - void videobuf_queue_vmalloc_init(struct videobuf_queue *q, 180 - struct videobuf_queue_ops *ops, 181 - struct device *dev, 182 - spinlock_t *irqlock, 183 - enum v4l2_buf_type type, 184 - enum v4l2_field field, 185 - unsigned int msize, 186 - void *priv); 187 - 188 - void videobuf_queue_dma_contig_init(struct videobuf_queue *q, 189 - struct videobuf_queue_ops *ops, 190 - struct device *dev, 191 - spinlock_t *irqlock, 192 - enum v4l2_buf_type type, 193 - enum v4l2_field field, 194 - unsigned int msize, 195 - void *priv); 196 - 197 - In each case, the parameters are the same: q is the queue structure for the 198 - device, ops is the set of callbacks as described above, dev is the device 199 - structure for this video device, irqlock is an interrupt-safe spinlock to 200 - protect access to the data structures, type is the buffer type used by the 201 - device (cameras will use V4L2_BUF_TYPE_VIDEO_CAPTURE, for example), field 202 - describes which field is being captured (often V4L2_FIELD_NONE for 203 - progressive devices), msize is the size of any containing structure used 204 - around struct videobuf_buffer, and priv is a private data pointer which 205 - shows up in the priv_data field of struct videobuf_queue. Note that these 206 - are void functions which, evidently, are immune to failure. 207 - 208 - V4L2 capture drivers can be written to support either of two APIs: the 209 - read() system call and the rather more complicated streaming mechanism. As 210 - a general rule, it is necessary to support both to ensure that all 211 - applications have a chance of working with the device. Videobuf makes it 212 - easy to do that with the same code. To implement read(), the driver need 213 - only make a call to one of: 214 - 215 - .. code-block:: none 216 - 217 - ssize_t videobuf_read_one(struct videobuf_queue *q, 218 - char __user *data, size_t count, 219 - loff_t *ppos, int nonblocking); 220 - 221 - ssize_t videobuf_read_stream(struct videobuf_queue *q, 222 - char __user *data, size_t count, 223 - loff_t *ppos, int vbihack, int nonblocking); 224 - 225 - Either one of these functions will read frame data into data, returning the 226 - amount actually read; the difference is that videobuf_read_one() will only 227 - read a single frame, while videobuf_read_stream() will read multiple frames 228 - if they are needed to satisfy the count requested by the application. A 229 - typical driver read() implementation will start the capture engine, call 230 - one of the above functions, then stop the engine before returning (though a 231 - smarter implementation might leave the engine running for a little while in 232 - anticipation of another read() call happening in the near future). 233 - 234 - The poll() function can usually be implemented with a direct call to: 235 - 236 - .. code-block:: none 237 - 238 - unsigned int videobuf_poll_stream(struct file *file, 239 - struct videobuf_queue *q, 240 - poll_table *wait); 241 - 242 - Note that the actual wait queue eventually used will be the one associated 243 - with the first available buffer. 244 - 245 - When streaming I/O is done to kernel-space buffers, the driver must support 246 - the mmap() system call to enable user space to access the data. In many 247 - V4L2 drivers, the often-complex mmap() implementation simplifies to a 248 - single call to: 249 - 250 - .. code-block:: none 251 - 252 - int videobuf_mmap_mapper(struct videobuf_queue *q, 253 - struct vm_area_struct *vma); 254 - 255 - Everything else is handled by the videobuf code. 256 - 257 - The release() function requires two separate videobuf calls: 258 - 259 - .. code-block:: none 260 - 261 - void videobuf_stop(struct videobuf_queue *q); 262 - int videobuf_mmap_free(struct videobuf_queue *q); 263 - 264 - The call to videobuf_stop() terminates any I/O in progress - though it is 265 - still up to the driver to stop the capture engine. The call to 266 - videobuf_mmap_free() will ensure that all buffers have been unmapped; if 267 - so, they will all be passed to the buf_release() callback. If buffers 268 - remain mapped, videobuf_mmap_free() returns an error code instead. The 269 - purpose is clearly to cause the closing of the file descriptor to fail if 270 - buffers are still mapped, but every driver in the 2.6.32 kernel cheerfully 271 - ignores its return value. 272 - 273 - ioctl() operations 274 - ------------------ 275 - 276 - The V4L2 API includes a very long list of driver callbacks to respond to 277 - the many ioctl() commands made available to user space. A number of these 278 - - those associated with streaming I/O - turn almost directly into videobuf 279 - calls. The relevant helper functions are: 280 - 281 - .. code-block:: none 282 - 283 - int videobuf_reqbufs(struct videobuf_queue *q, 284 - struct v4l2_requestbuffers *req); 285 - int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); 286 - int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b); 287 - int videobuf_dqbuf(struct videobuf_queue *q, struct v4l2_buffer *b, 288 - int nonblocking); 289 - int videobuf_streamon(struct videobuf_queue *q); 290 - int videobuf_streamoff(struct videobuf_queue *q); 291 - 292 - So, for example, a VIDIOC_REQBUFS call turns into a call to the driver's 293 - vidioc_reqbufs() callback which, in turn, usually only needs to locate the 294 - proper struct videobuf_queue pointer and pass it to videobuf_reqbufs(). 295 - These support functions can replace a great deal of buffer management 296 - boilerplate in a lot of V4L2 drivers. 297 - 298 - The vidioc_streamon() and vidioc_streamoff() functions will be a bit more 299 - complex, of course, since they will also need to deal with starting and 300 - stopping the capture engine. 301 - 302 - Buffer allocation 303 - ----------------- 304 - 305 - Thus far, we have talked about buffers, but have not looked at how they are 306 - allocated. The scatter/gather case is the most complex on this front. For 307 - allocation, the driver can leave buffer allocation entirely up to the 308 - videobuf layer; in this case, buffers will be allocated as anonymous 309 - user-space pages and will be very scattered indeed. If the application is 310 - using user-space buffers, no allocation is needed; the videobuf layer will 311 - take care of calling get_user_pages() and filling in the scatterlist array. 312 - 313 - If the driver needs to do its own memory allocation, it should be done in 314 - the vidioc_reqbufs() function, *after* calling videobuf_reqbufs(). The 315 - first step is a call to: 316 - 317 - .. code-block:: none 318 - 319 - struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf); 320 - 321 - The returned videobuf_dmabuf structure (defined in 322 - <media/videobuf-dma-sg.h>) includes a couple of relevant fields: 323 - 324 - .. code-block:: none 325 - 326 - struct scatterlist *sglist; 327 - int sglen; 328 - 329 - The driver must allocate an appropriately-sized scatterlist array and 330 - populate it with pointers to the pieces of the allocated buffer; sglen 331 - should be set to the length of the array. 332 - 333 - Drivers using the vmalloc() method need not (and cannot) concern themselves 334 - with buffer allocation at all; videobuf will handle those details. The 335 - same is normally true of contiguous-DMA drivers as well; videobuf will 336 - allocate the buffers (with dma_alloc_coherent()) when it sees fit. That 337 - means that these drivers may be trying to do high-order allocations at any 338 - time, an operation which is not always guaranteed to work. Some drivers 339 - play tricks by allocating DMA space at system boot time; videobuf does not 340 - currently play well with those drivers. 341 - 342 - As of 2.6.31, contiguous-DMA drivers can work with a user-supplied buffer, 343 - as long as that buffer is physically contiguous. Normal user-space 344 - allocations will not meet that criterion, but buffers obtained from other 345 - kernel drivers, or those contained within huge pages, will work with these 346 - drivers. 347 - 348 - Filling the buffers 349 - ------------------- 350 - 351 - The final part of a videobuf implementation has no direct callback - it's 352 - the portion of the code which actually puts frame data into the buffers, 353 - usually in response to interrupts from the device. For all types of 354 - drivers, this process works approximately as follows: 355 - 356 - - Obtain the next available buffer and make sure that somebody is actually 357 - waiting for it. 358 - 359 - - Get a pointer to the memory and put video data there. 360 - 361 - - Mark the buffer as done and wake up the process waiting for it. 362 - 363 - Step (1) above is done by looking at the driver-managed list_head structure 364 - - the one which is filled in the buf_queue() callback. Because starting 365 - the engine and enqueueing buffers are done in separate steps, it's possible 366 - for the engine to be running without any buffers available - in the 367 - vmalloc() case especially. So the driver should be prepared for the list 368 - to be empty. It is equally possible that nobody is yet interested in the 369 - buffer; the driver should not remove it from the list or fill it until a 370 - process is waiting on it. That test can be done by examining the buffer's 371 - done field (a wait_queue_head_t structure) with waitqueue_active(). 372 - 373 - A buffer's state should be set to VIDEOBUF_ACTIVE before being mapped for 374 - DMA; that ensures that the videobuf layer will not try to do anything with 375 - it while the device is transferring data. 376 - 377 - For scatter/gather drivers, the needed memory pointers will be found in the 378 - scatterlist structure described above. Drivers using the vmalloc() method 379 - can get a memory pointer with: 380 - 381 - .. code-block:: none 382 - 383 - void *videobuf_to_vmalloc(struct videobuf_buffer *buf); 384 - 385 - For contiguous DMA drivers, the function to use is: 386 - 387 - .. code-block:: none 388 - 389 - dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf); 390 - 391 - The contiguous DMA API goes out of its way to hide the kernel-space address 392 - of the DMA buffer from drivers. 393 - 394 - The final step is to set the size field of the relevant videobuf_buffer 395 - structure to the actual size of the captured image, set state to 396 - VIDEOBUF_DONE, then call wake_up() on the done queue. At this point, the 397 - buffer is owned by the videobuf layer and the driver should not touch it 398 - again. 399 - 400 - Developers who are interested in more information can go into the relevant 401 - header files; there are a few low-level functions declared there which have 402 - not been talked about here. Note also that all of these calls are exported 403 - GPL-only, so they will not be available to non-GPL kernel modules.
-12
Documentation/translations/zh_CN/video4linux/v4l2-framework.txt
··· 768 768 此功能,而非访问 video_device::num 和 video_device::minor 域。 769 769 770 770 771 - 视频缓冲辅助函数 772 - --------------- 773 - 774 - v4l2 核心 API 提供了一个处理视频缓冲的标准方法(称为“videobuf”)。 775 - 这些方法使驱动可以通过统一的方式实现 read()、mmap() 和 overlay()。 776 - 目前在设备上支持视频缓冲的方法有分散/聚集 DMA(videobuf-dma-sg)、 777 - 线性 DMA(videobuf-dma-contig)以及大多用于 USB 设备的用 vmalloc 778 - 分配的缓冲(videobuf-vmalloc)。 779 - 780 - 请参阅 Documentation/driver-api/media/v4l2-videobuf.rst,以获得更多关于 videobuf 781 - 层的使用信息。 782 - 783 771 v4l2_fh 结构体 784 772 ------------- 785 773
-16
drivers/media/v4l2-core/Kconfig
··· 82 82 depends on I2C 83 83 select REGMAP_I2C 84 84 select V4L2_CCI 85 - 86 - # Used by drivers that need Videobuf modules 87 - config VIDEOBUF_GEN 88 - tristate 89 - 90 - config VIDEOBUF_DMA_SG 91 - tristate 92 - select VIDEOBUF_GEN 93 - 94 - config VIDEOBUF_VMALLOC 95 - tristate 96 - select VIDEOBUF_GEN 97 - 98 - config VIDEOBUF_DMA_CONTIG 99 - tristate 100 - select VIDEOBUF_GEN
-5
drivers/media/v4l2-core/Makefile
··· 33 33 obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o 34 34 obj-$(CONFIG_V4L2_VP9) += v4l2-vp9.o 35 35 36 - obj-$(CONFIG_VIDEOBUF_DMA_CONTIG) += videobuf-dma-contig.o 37 - obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o 38 - obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o 39 - obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o 40 - 41 36 obj-$(CONFIG_VIDEO_TUNER) += tuner.o 42 37 obj-$(CONFIG_VIDEO_DEV) += v4l2-dv-timings.o videodev.o
-1198
drivers/media/v4l2-core/videobuf-core.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * generic helper functions for handling video4linux capture buffers 4 - * 5 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 6 - * 7 - * Highly based on video-buf written originally by: 8 - * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> 9 - * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org> 10 - * (c) 2006 Ted Walther and John Sokol 11 - */ 12 - 13 - #include <linux/init.h> 14 - #include <linux/module.h> 15 - #include <linux/moduleparam.h> 16 - #include <linux/mm.h> 17 - #include <linux/sched.h> 18 - #include <linux/slab.h> 19 - #include <linux/interrupt.h> 20 - 21 - #include <media/videobuf-core.h> 22 - #include <media/v4l2-common.h> 23 - 24 - #define MAGIC_BUFFER 0x20070728 25 - #define MAGIC_CHECK(is, should) \ 26 - do { \ 27 - if (unlikely((is) != (should))) { \ 28 - printk(KERN_ERR \ 29 - "magic mismatch: %x (expected %x)\n", \ 30 - is, should); \ 31 - BUG(); \ 32 - } \ 33 - } while (0) 34 - 35 - static int debug; 36 - module_param(debug, int, 0644); 37 - 38 - MODULE_DESCRIPTION("helper module to manage video4linux buffers"); 39 - MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>"); 40 - MODULE_LICENSE("GPL"); 41 - 42 - #define dprintk(level, fmt, arg...) \ 43 - do { \ 44 - if (debug >= level) \ 45 - printk(KERN_DEBUG "vbuf: " fmt, ## arg); \ 46 - } while (0) 47 - 48 - /* --------------------------------------------------------------------- */ 49 - 50 - #define CALL(q, f, arg...) \ 51 - ((q->int_ops->f) ? q->int_ops->f(arg) : 0) 52 - #define CALLPTR(q, f, arg...) \ 53 - ((q->int_ops->f) ? q->int_ops->f(arg) : NULL) 54 - 55 - struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q) 56 - { 57 - struct videobuf_buffer *vb; 58 - 59 - BUG_ON(q->msize < sizeof(*vb)); 60 - 61 - if (!q->int_ops || !q->int_ops->alloc_vb) { 62 - printk(KERN_ERR "No specific ops defined!\n"); 63 - BUG(); 64 - } 65 - 66 - vb = q->int_ops->alloc_vb(q->msize); 67 - if (NULL != vb) { 68 - init_waitqueue_head(&vb->done); 69 - vb->magic = MAGIC_BUFFER; 70 - } 71 - 72 - return vb; 73 - } 74 - EXPORT_SYMBOL_GPL(videobuf_alloc_vb); 75 - 76 - static int state_neither_active_nor_queued(struct videobuf_queue *q, 77 - struct videobuf_buffer *vb) 78 - { 79 - unsigned long flags; 80 - bool rc; 81 - 82 - spin_lock_irqsave(q->irqlock, flags); 83 - rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED; 84 - spin_unlock_irqrestore(q->irqlock, flags); 85 - return rc; 86 - }; 87 - 88 - int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb, 89 - int non_blocking, int intr) 90 - { 91 - bool is_ext_locked; 92 - int ret = 0; 93 - 94 - MAGIC_CHECK(vb->magic, MAGIC_BUFFER); 95 - 96 - if (non_blocking) { 97 - if (state_neither_active_nor_queued(q, vb)) 98 - return 0; 99 - return -EAGAIN; 100 - } 101 - 102 - is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock); 103 - 104 - /* Release vdev lock to prevent this wait from blocking outside access to 105 - the device. */ 106 - if (is_ext_locked) 107 - mutex_unlock(q->ext_lock); 108 - if (intr) 109 - ret = wait_event_interruptible(vb->done, 110 - state_neither_active_nor_queued(q, vb)); 111 - else 112 - wait_event(vb->done, state_neither_active_nor_queued(q, vb)); 113 - /* Relock */ 114 - if (is_ext_locked) 115 - mutex_lock(q->ext_lock); 116 - 117 - return ret; 118 - } 119 - EXPORT_SYMBOL_GPL(videobuf_waiton); 120 - 121 - int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, 122 - struct v4l2_framebuffer *fbuf) 123 - { 124 - MAGIC_CHECK(vb->magic, MAGIC_BUFFER); 125 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 126 - 127 - return CALL(q, iolock, q, vb, fbuf); 128 - } 129 - EXPORT_SYMBOL_GPL(videobuf_iolock); 130 - 131 - void *videobuf_queue_to_vaddr(struct videobuf_queue *q, 132 - struct videobuf_buffer *buf) 133 - { 134 - if (q->int_ops->vaddr) 135 - return q->int_ops->vaddr(buf); 136 - return NULL; 137 - } 138 - EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr); 139 - 140 - /* --------------------------------------------------------------------- */ 141 - 142 - 143 - void videobuf_queue_core_init(struct videobuf_queue *q, 144 - const struct videobuf_queue_ops *ops, 145 - struct device *dev, 146 - spinlock_t *irqlock, 147 - enum v4l2_buf_type type, 148 - enum v4l2_field field, 149 - unsigned int msize, 150 - void *priv, 151 - struct videobuf_qtype_ops *int_ops, 152 - struct mutex *ext_lock) 153 - { 154 - BUG_ON(!q); 155 - memset(q, 0, sizeof(*q)); 156 - q->irqlock = irqlock; 157 - q->ext_lock = ext_lock; 158 - q->dev = dev; 159 - q->type = type; 160 - q->field = field; 161 - q->msize = msize; 162 - q->ops = ops; 163 - q->priv_data = priv; 164 - q->int_ops = int_ops; 165 - 166 - /* All buffer operations are mandatory */ 167 - BUG_ON(!q->ops->buf_setup); 168 - BUG_ON(!q->ops->buf_prepare); 169 - BUG_ON(!q->ops->buf_queue); 170 - BUG_ON(!q->ops->buf_release); 171 - 172 - /* Lock is mandatory for queue_cancel to work */ 173 - BUG_ON(!irqlock); 174 - 175 - /* Having implementations for abstract methods are mandatory */ 176 - BUG_ON(!q->int_ops); 177 - 178 - mutex_init(&q->vb_lock); 179 - init_waitqueue_head(&q->wait); 180 - INIT_LIST_HEAD(&q->stream); 181 - } 182 - EXPORT_SYMBOL_GPL(videobuf_queue_core_init); 183 - 184 - /* Locking: Only usage in bttv unsafe find way to remove */ 185 - int videobuf_queue_is_busy(struct videobuf_queue *q) 186 - { 187 - int i; 188 - 189 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 190 - 191 - if (q->streaming) { 192 - dprintk(1, "busy: streaming active\n"); 193 - return 1; 194 - } 195 - if (q->reading) { 196 - dprintk(1, "busy: pending read #1\n"); 197 - return 1; 198 - } 199 - if (q->read_buf) { 200 - dprintk(1, "busy: pending read #2\n"); 201 - return 1; 202 - } 203 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 204 - if (NULL == q->bufs[i]) 205 - continue; 206 - if (q->bufs[i]->map) { 207 - dprintk(1, "busy: buffer #%d mapped\n", i); 208 - return 1; 209 - } 210 - if (q->bufs[i]->state == VIDEOBUF_QUEUED) { 211 - dprintk(1, "busy: buffer #%d queued\n", i); 212 - return 1; 213 - } 214 - if (q->bufs[i]->state == VIDEOBUF_ACTIVE) { 215 - dprintk(1, "busy: buffer #%d active\n", i); 216 - return 1; 217 - } 218 - } 219 - return 0; 220 - } 221 - EXPORT_SYMBOL_GPL(videobuf_queue_is_busy); 222 - 223 - /* 224 - * __videobuf_free() - free all the buffers and their control structures 225 - * 226 - * This function can only be called if streaming/reading is off, i.e. no buffers 227 - * are under control of the driver. 228 - */ 229 - /* Locking: Caller holds q->vb_lock */ 230 - static int __videobuf_free(struct videobuf_queue *q) 231 - { 232 - int i; 233 - 234 - dprintk(1, "%s\n", __func__); 235 - if (!q) 236 - return 0; 237 - 238 - if (q->streaming || q->reading) { 239 - dprintk(1, "Cannot free buffers when streaming or reading\n"); 240 - return -EBUSY; 241 - } 242 - 243 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 244 - 245 - for (i = 0; i < VIDEO_MAX_FRAME; i++) 246 - if (q->bufs[i] && q->bufs[i]->map) { 247 - dprintk(1, "Cannot free mmapped buffers\n"); 248 - return -EBUSY; 249 - } 250 - 251 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 252 - if (NULL == q->bufs[i]) 253 - continue; 254 - q->ops->buf_release(q, q->bufs[i]); 255 - kfree(q->bufs[i]); 256 - q->bufs[i] = NULL; 257 - } 258 - 259 - return 0; 260 - } 261 - 262 - /* Locking: Caller holds q->vb_lock */ 263 - void videobuf_queue_cancel(struct videobuf_queue *q) 264 - { 265 - unsigned long flags = 0; 266 - int i; 267 - 268 - q->streaming = 0; 269 - q->reading = 0; 270 - wake_up_interruptible_sync(&q->wait); 271 - 272 - /* remove queued buffers from list */ 273 - spin_lock_irqsave(q->irqlock, flags); 274 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 275 - if (NULL == q->bufs[i]) 276 - continue; 277 - if (q->bufs[i]->state == VIDEOBUF_QUEUED) { 278 - list_del(&q->bufs[i]->queue); 279 - q->bufs[i]->state = VIDEOBUF_ERROR; 280 - wake_up_all(&q->bufs[i]->done); 281 - } 282 - } 283 - spin_unlock_irqrestore(q->irqlock, flags); 284 - 285 - /* free all buffers + clear queue */ 286 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 287 - if (NULL == q->bufs[i]) 288 - continue; 289 - q->ops->buf_release(q, q->bufs[i]); 290 - } 291 - INIT_LIST_HEAD(&q->stream); 292 - } 293 - EXPORT_SYMBOL_GPL(videobuf_queue_cancel); 294 - 295 - /* --------------------------------------------------------------------- */ 296 - 297 - /* Locking: Caller holds q->vb_lock */ 298 - enum v4l2_field videobuf_next_field(struct videobuf_queue *q) 299 - { 300 - enum v4l2_field field = q->field; 301 - 302 - BUG_ON(V4L2_FIELD_ANY == field); 303 - 304 - if (V4L2_FIELD_ALTERNATE == field) { 305 - if (V4L2_FIELD_TOP == q->last) { 306 - field = V4L2_FIELD_BOTTOM; 307 - q->last = V4L2_FIELD_BOTTOM; 308 - } else { 309 - field = V4L2_FIELD_TOP; 310 - q->last = V4L2_FIELD_TOP; 311 - } 312 - } 313 - return field; 314 - } 315 - EXPORT_SYMBOL_GPL(videobuf_next_field); 316 - 317 - /* Locking: Caller holds q->vb_lock */ 318 - static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b, 319 - struct videobuf_buffer *vb, enum v4l2_buf_type type) 320 - { 321 - MAGIC_CHECK(vb->magic, MAGIC_BUFFER); 322 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 323 - 324 - b->index = vb->i; 325 - b->type = type; 326 - 327 - b->memory = vb->memory; 328 - switch (b->memory) { 329 - case V4L2_MEMORY_MMAP: 330 - b->m.offset = vb->boff; 331 - b->length = vb->bsize; 332 - break; 333 - case V4L2_MEMORY_USERPTR: 334 - b->m.userptr = vb->baddr; 335 - b->length = vb->bsize; 336 - break; 337 - case V4L2_MEMORY_OVERLAY: 338 - b->m.offset = vb->boff; 339 - break; 340 - case V4L2_MEMORY_DMABUF: 341 - /* DMABUF is not handled in videobuf framework */ 342 - break; 343 - } 344 - 345 - b->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 346 - if (vb->map) 347 - b->flags |= V4L2_BUF_FLAG_MAPPED; 348 - 349 - switch (vb->state) { 350 - case VIDEOBUF_PREPARED: 351 - case VIDEOBUF_QUEUED: 352 - case VIDEOBUF_ACTIVE: 353 - b->flags |= V4L2_BUF_FLAG_QUEUED; 354 - break; 355 - case VIDEOBUF_ERROR: 356 - b->flags |= V4L2_BUF_FLAG_ERROR; 357 - fallthrough; 358 - case VIDEOBUF_DONE: 359 - b->flags |= V4L2_BUF_FLAG_DONE; 360 - break; 361 - case VIDEOBUF_NEEDS_INIT: 362 - case VIDEOBUF_IDLE: 363 - /* nothing */ 364 - break; 365 - } 366 - 367 - b->field = vb->field; 368 - v4l2_buffer_set_timestamp(b, vb->ts); 369 - b->bytesused = vb->size; 370 - b->sequence = vb->field_count >> 1; 371 - } 372 - 373 - int videobuf_mmap_free(struct videobuf_queue *q) 374 - { 375 - int ret; 376 - videobuf_queue_lock(q); 377 - ret = __videobuf_free(q); 378 - videobuf_queue_unlock(q); 379 - return ret; 380 - } 381 - EXPORT_SYMBOL_GPL(videobuf_mmap_free); 382 - 383 - /* Locking: Caller holds q->vb_lock */ 384 - int __videobuf_mmap_setup(struct videobuf_queue *q, 385 - unsigned int bcount, unsigned int bsize, 386 - enum v4l2_memory memory) 387 - { 388 - unsigned int i; 389 - int err; 390 - 391 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 392 - 393 - err = __videobuf_free(q); 394 - if (0 != err) 395 - return err; 396 - 397 - /* Allocate and initialize buffers */ 398 - for (i = 0; i < bcount; i++) { 399 - q->bufs[i] = videobuf_alloc_vb(q); 400 - 401 - if (NULL == q->bufs[i]) 402 - break; 403 - 404 - q->bufs[i]->i = i; 405 - q->bufs[i]->memory = memory; 406 - q->bufs[i]->bsize = bsize; 407 - switch (memory) { 408 - case V4L2_MEMORY_MMAP: 409 - q->bufs[i]->boff = PAGE_ALIGN(bsize) * i; 410 - break; 411 - case V4L2_MEMORY_USERPTR: 412 - case V4L2_MEMORY_OVERLAY: 413 - case V4L2_MEMORY_DMABUF: 414 - /* nothing */ 415 - break; 416 - } 417 - } 418 - 419 - if (!i) 420 - return -ENOMEM; 421 - 422 - dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize); 423 - 424 - return i; 425 - } 426 - EXPORT_SYMBOL_GPL(__videobuf_mmap_setup); 427 - 428 - int videobuf_mmap_setup(struct videobuf_queue *q, 429 - unsigned int bcount, unsigned int bsize, 430 - enum v4l2_memory memory) 431 - { 432 - int ret; 433 - videobuf_queue_lock(q); 434 - ret = __videobuf_mmap_setup(q, bcount, bsize, memory); 435 - videobuf_queue_unlock(q); 436 - return ret; 437 - } 438 - EXPORT_SYMBOL_GPL(videobuf_mmap_setup); 439 - 440 - int videobuf_reqbufs(struct videobuf_queue *q, 441 - struct v4l2_requestbuffers *req) 442 - { 443 - unsigned int size, count; 444 - int retval; 445 - 446 - if (req->memory != V4L2_MEMORY_MMAP && 447 - req->memory != V4L2_MEMORY_USERPTR && 448 - req->memory != V4L2_MEMORY_OVERLAY) { 449 - dprintk(1, "reqbufs: memory type invalid\n"); 450 - return -EINVAL; 451 - } 452 - 453 - videobuf_queue_lock(q); 454 - if (req->type != q->type) { 455 - dprintk(1, "reqbufs: queue type invalid\n"); 456 - retval = -EINVAL; 457 - goto done; 458 - } 459 - 460 - if (q->streaming) { 461 - dprintk(1, "reqbufs: streaming already exists\n"); 462 - retval = -EBUSY; 463 - goto done; 464 - } 465 - if (!list_empty(&q->stream)) { 466 - dprintk(1, "reqbufs: stream running\n"); 467 - retval = -EBUSY; 468 - goto done; 469 - } 470 - 471 - if (req->count == 0) { 472 - dprintk(1, "reqbufs: count invalid (%d)\n", req->count); 473 - retval = __videobuf_free(q); 474 - goto done; 475 - } 476 - 477 - count = req->count; 478 - if (count > VIDEO_MAX_FRAME) 479 - count = VIDEO_MAX_FRAME; 480 - size = 0; 481 - q->ops->buf_setup(q, &count, &size); 482 - dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n", 483 - count, size, 484 - (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT)); 485 - 486 - retval = __videobuf_mmap_setup(q, count, size, req->memory); 487 - if (retval < 0) { 488 - dprintk(1, "reqbufs: mmap setup returned %d\n", retval); 489 - goto done; 490 - } 491 - 492 - req->count = retval; 493 - retval = 0; 494 - 495 - done: 496 - videobuf_queue_unlock(q); 497 - return retval; 498 - } 499 - EXPORT_SYMBOL_GPL(videobuf_reqbufs); 500 - 501 - int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b) 502 - { 503 - int ret = -EINVAL; 504 - 505 - videobuf_queue_lock(q); 506 - if (unlikely(b->type != q->type)) { 507 - dprintk(1, "querybuf: Wrong type.\n"); 508 - goto done; 509 - } 510 - if (unlikely(b->index >= VIDEO_MAX_FRAME)) { 511 - dprintk(1, "querybuf: index out of range.\n"); 512 - goto done; 513 - } 514 - if (unlikely(NULL == q->bufs[b->index])) { 515 - dprintk(1, "querybuf: buffer is null.\n"); 516 - goto done; 517 - } 518 - 519 - videobuf_status(q, b, q->bufs[b->index], q->type); 520 - 521 - ret = 0; 522 - done: 523 - videobuf_queue_unlock(q); 524 - return ret; 525 - } 526 - EXPORT_SYMBOL_GPL(videobuf_querybuf); 527 - 528 - int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b) 529 - { 530 - struct videobuf_buffer *buf; 531 - enum v4l2_field field; 532 - unsigned long flags = 0; 533 - int retval; 534 - 535 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 536 - 537 - if (b->memory == V4L2_MEMORY_MMAP) 538 - mmap_read_lock(current->mm); 539 - 540 - videobuf_queue_lock(q); 541 - retval = -EBUSY; 542 - if (q->reading) { 543 - dprintk(1, "qbuf: Reading running...\n"); 544 - goto done; 545 - } 546 - retval = -EINVAL; 547 - if (b->type != q->type) { 548 - dprintk(1, "qbuf: Wrong type.\n"); 549 - goto done; 550 - } 551 - if (b->index >= VIDEO_MAX_FRAME) { 552 - dprintk(1, "qbuf: index out of range.\n"); 553 - goto done; 554 - } 555 - buf = q->bufs[b->index]; 556 - if (NULL == buf) { 557 - dprintk(1, "qbuf: buffer is null.\n"); 558 - goto done; 559 - } 560 - MAGIC_CHECK(buf->magic, MAGIC_BUFFER); 561 - if (buf->memory != b->memory) { 562 - dprintk(1, "qbuf: memory type is wrong.\n"); 563 - goto done; 564 - } 565 - if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) { 566 - dprintk(1, "qbuf: buffer is already queued or active.\n"); 567 - goto done; 568 - } 569 - 570 - switch (b->memory) { 571 - case V4L2_MEMORY_MMAP: 572 - if (0 == buf->baddr) { 573 - dprintk(1, "qbuf: mmap requested but buffer addr is zero!\n"); 574 - goto done; 575 - } 576 - if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT 577 - || q->type == V4L2_BUF_TYPE_VBI_OUTPUT 578 - || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT 579 - || q->type == V4L2_BUF_TYPE_SDR_OUTPUT) { 580 - buf->size = b->bytesused; 581 - buf->field = b->field; 582 - buf->ts = v4l2_buffer_get_timestamp(b); 583 - } 584 - break; 585 - case V4L2_MEMORY_USERPTR: 586 - if (b->length < buf->bsize) { 587 - dprintk(1, "qbuf: buffer length is not enough\n"); 588 - goto done; 589 - } 590 - if (VIDEOBUF_NEEDS_INIT != buf->state && 591 - buf->baddr != b->m.userptr) 592 - q->ops->buf_release(q, buf); 593 - buf->baddr = b->m.userptr; 594 - break; 595 - case V4L2_MEMORY_OVERLAY: 596 - buf->boff = b->m.offset; 597 - break; 598 - default: 599 - dprintk(1, "qbuf: wrong memory type\n"); 600 - goto done; 601 - } 602 - 603 - dprintk(1, "qbuf: requesting next field\n"); 604 - field = videobuf_next_field(q); 605 - retval = q->ops->buf_prepare(q, buf, field); 606 - if (0 != retval) { 607 - dprintk(1, "qbuf: buffer_prepare returned %d\n", retval); 608 - goto done; 609 - } 610 - 611 - list_add_tail(&buf->stream, &q->stream); 612 - if (q->streaming) { 613 - spin_lock_irqsave(q->irqlock, flags); 614 - q->ops->buf_queue(q, buf); 615 - spin_unlock_irqrestore(q->irqlock, flags); 616 - } 617 - dprintk(1, "qbuf: succeeded\n"); 618 - retval = 0; 619 - wake_up_interruptible_sync(&q->wait); 620 - 621 - done: 622 - videobuf_queue_unlock(q); 623 - 624 - if (b->memory == V4L2_MEMORY_MMAP) 625 - mmap_read_unlock(current->mm); 626 - 627 - return retval; 628 - } 629 - EXPORT_SYMBOL_GPL(videobuf_qbuf); 630 - 631 - /* Locking: Caller holds q->vb_lock */ 632 - static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock) 633 - { 634 - int retval; 635 - 636 - checks: 637 - if (!q->streaming) { 638 - dprintk(1, "next_buffer: Not streaming\n"); 639 - retval = -EINVAL; 640 - goto done; 641 - } 642 - 643 - if (list_empty(&q->stream)) { 644 - if (noblock) { 645 - retval = -EAGAIN; 646 - dprintk(2, "next_buffer: no buffers to dequeue\n"); 647 - goto done; 648 - } else { 649 - dprintk(2, "next_buffer: waiting on buffer\n"); 650 - 651 - /* Drop lock to avoid deadlock with qbuf */ 652 - videobuf_queue_unlock(q); 653 - 654 - /* Checking list_empty and streaming is safe without 655 - * locks because we goto checks to validate while 656 - * holding locks before proceeding */ 657 - retval = wait_event_interruptible(q->wait, 658 - !list_empty(&q->stream) || !q->streaming); 659 - videobuf_queue_lock(q); 660 - 661 - if (retval) 662 - goto done; 663 - 664 - goto checks; 665 - } 666 - } 667 - 668 - retval = 0; 669 - 670 - done: 671 - return retval; 672 - } 673 - 674 - /* Locking: Caller holds q->vb_lock */ 675 - static int stream_next_buffer(struct videobuf_queue *q, 676 - struct videobuf_buffer **vb, int nonblocking) 677 - { 678 - int retval; 679 - struct videobuf_buffer *buf = NULL; 680 - 681 - retval = stream_next_buffer_check_queue(q, nonblocking); 682 - if (retval) 683 - goto done; 684 - 685 - buf = list_entry(q->stream.next, struct videobuf_buffer, stream); 686 - retval = videobuf_waiton(q, buf, nonblocking, 1); 687 - if (retval < 0) 688 - goto done; 689 - 690 - *vb = buf; 691 - done: 692 - return retval; 693 - } 694 - 695 - int videobuf_dqbuf(struct videobuf_queue *q, 696 - struct v4l2_buffer *b, int nonblocking) 697 - { 698 - struct videobuf_buffer *buf = NULL; 699 - int retval; 700 - 701 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 702 - 703 - memset(b, 0, sizeof(*b)); 704 - videobuf_queue_lock(q); 705 - 706 - retval = stream_next_buffer(q, &buf, nonblocking); 707 - if (retval < 0) { 708 - dprintk(1, "dqbuf: next_buffer error: %i\n", retval); 709 - goto done; 710 - } 711 - 712 - switch (buf->state) { 713 - case VIDEOBUF_ERROR: 714 - dprintk(1, "dqbuf: state is error\n"); 715 - break; 716 - case VIDEOBUF_DONE: 717 - dprintk(1, "dqbuf: state is done\n"); 718 - break; 719 - default: 720 - dprintk(1, "dqbuf: state invalid\n"); 721 - retval = -EINVAL; 722 - goto done; 723 - } 724 - CALL(q, sync, q, buf); 725 - videobuf_status(q, b, buf, q->type); 726 - list_del(&buf->stream); 727 - buf->state = VIDEOBUF_IDLE; 728 - b->flags &= ~V4L2_BUF_FLAG_DONE; 729 - done: 730 - videobuf_queue_unlock(q); 731 - return retval; 732 - } 733 - EXPORT_SYMBOL_GPL(videobuf_dqbuf); 734 - 735 - int videobuf_streamon(struct videobuf_queue *q) 736 - { 737 - struct videobuf_buffer *buf; 738 - unsigned long flags = 0; 739 - int retval; 740 - 741 - videobuf_queue_lock(q); 742 - retval = -EBUSY; 743 - if (q->reading) 744 - goto done; 745 - retval = 0; 746 - if (q->streaming) 747 - goto done; 748 - q->streaming = 1; 749 - spin_lock_irqsave(q->irqlock, flags); 750 - list_for_each_entry(buf, &q->stream, stream) 751 - if (buf->state == VIDEOBUF_PREPARED) 752 - q->ops->buf_queue(q, buf); 753 - spin_unlock_irqrestore(q->irqlock, flags); 754 - 755 - wake_up_interruptible_sync(&q->wait); 756 - done: 757 - videobuf_queue_unlock(q); 758 - return retval; 759 - } 760 - EXPORT_SYMBOL_GPL(videobuf_streamon); 761 - 762 - /* Locking: Caller holds q->vb_lock */ 763 - static int __videobuf_streamoff(struct videobuf_queue *q) 764 - { 765 - if (!q->streaming) 766 - return -EINVAL; 767 - 768 - videobuf_queue_cancel(q); 769 - 770 - return 0; 771 - } 772 - 773 - int videobuf_streamoff(struct videobuf_queue *q) 774 - { 775 - int retval; 776 - 777 - videobuf_queue_lock(q); 778 - retval = __videobuf_streamoff(q); 779 - videobuf_queue_unlock(q); 780 - 781 - return retval; 782 - } 783 - EXPORT_SYMBOL_GPL(videobuf_streamoff); 784 - 785 - /* Locking: Caller holds q->vb_lock */ 786 - static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q, 787 - char __user *data, 788 - size_t count, loff_t *ppos) 789 - { 790 - enum v4l2_field field; 791 - unsigned long flags = 0; 792 - int retval; 793 - 794 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 795 - 796 - /* setup stuff */ 797 - q->read_buf = videobuf_alloc_vb(q); 798 - if (NULL == q->read_buf) 799 - return -ENOMEM; 800 - 801 - q->read_buf->memory = V4L2_MEMORY_USERPTR; 802 - q->read_buf->baddr = (unsigned long)data; 803 - q->read_buf->bsize = count; 804 - 805 - field = videobuf_next_field(q); 806 - retval = q->ops->buf_prepare(q, q->read_buf, field); 807 - if (0 != retval) 808 - goto done; 809 - 810 - /* start capture & wait */ 811 - spin_lock_irqsave(q->irqlock, flags); 812 - q->ops->buf_queue(q, q->read_buf); 813 - spin_unlock_irqrestore(q->irqlock, flags); 814 - retval = videobuf_waiton(q, q->read_buf, 0, 0); 815 - if (0 == retval) { 816 - CALL(q, sync, q, q->read_buf); 817 - if (VIDEOBUF_ERROR == q->read_buf->state) 818 - retval = -EIO; 819 - else 820 - retval = q->read_buf->size; 821 - } 822 - 823 - done: 824 - /* cleanup */ 825 - q->ops->buf_release(q, q->read_buf); 826 - kfree(q->read_buf); 827 - q->read_buf = NULL; 828 - return retval; 829 - } 830 - 831 - static int __videobuf_copy_to_user(struct videobuf_queue *q, 832 - struct videobuf_buffer *buf, 833 - char __user *data, size_t count, 834 - int nonblocking) 835 - { 836 - void *vaddr = CALLPTR(q, vaddr, buf); 837 - 838 - /* copy to userspace */ 839 - if (count > buf->size - q->read_off) 840 - count = buf->size - q->read_off; 841 - 842 - if (copy_to_user(data, vaddr + q->read_off, count)) 843 - return -EFAULT; 844 - 845 - return count; 846 - } 847 - 848 - static int __videobuf_copy_stream(struct videobuf_queue *q, 849 - struct videobuf_buffer *buf, 850 - char __user *data, size_t count, size_t pos, 851 - int vbihack, int nonblocking) 852 - { 853 - unsigned int *fc = CALLPTR(q, vaddr, buf); 854 - 855 - if (vbihack) { 856 - /* dirty, undocumented hack -- pass the frame counter 857 - * within the last four bytes of each vbi data block. 858 - * We need that one to maintain backward compatibility 859 - * to all vbi decoding software out there ... */ 860 - fc += (buf->size >> 2) - 1; 861 - *fc = buf->field_count >> 1; 862 - dprintk(1, "vbihack: %d\n", *fc); 863 - } 864 - 865 - /* copy stuff using the common method */ 866 - count = __videobuf_copy_to_user(q, buf, data, count, nonblocking); 867 - 868 - if ((count == -EFAULT) && (pos == 0)) 869 - return -EFAULT; 870 - 871 - return count; 872 - } 873 - 874 - ssize_t videobuf_read_one(struct videobuf_queue *q, 875 - char __user *data, size_t count, loff_t *ppos, 876 - int nonblocking) 877 - { 878 - enum v4l2_field field; 879 - unsigned long flags = 0; 880 - unsigned size = 0, nbufs = 1; 881 - int retval; 882 - 883 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 884 - 885 - videobuf_queue_lock(q); 886 - 887 - q->ops->buf_setup(q, &nbufs, &size); 888 - 889 - if (NULL == q->read_buf && 890 - count >= size && 891 - !nonblocking) { 892 - retval = videobuf_read_zerocopy(q, data, count, ppos); 893 - if (retval >= 0 || retval == -EIO) 894 - /* ok, all done */ 895 - goto done; 896 - /* fallback to kernel bounce buffer on failures */ 897 - } 898 - 899 - if (NULL == q->read_buf) { 900 - /* need to capture a new frame */ 901 - retval = -ENOMEM; 902 - q->read_buf = videobuf_alloc_vb(q); 903 - 904 - dprintk(1, "video alloc=0x%p\n", q->read_buf); 905 - if (NULL == q->read_buf) 906 - goto done; 907 - q->read_buf->memory = V4L2_MEMORY_USERPTR; 908 - q->read_buf->bsize = count; /* preferred size */ 909 - field = videobuf_next_field(q); 910 - retval = q->ops->buf_prepare(q, q->read_buf, field); 911 - 912 - if (0 != retval) { 913 - kfree(q->read_buf); 914 - q->read_buf = NULL; 915 - goto done; 916 - } 917 - 918 - spin_lock_irqsave(q->irqlock, flags); 919 - q->ops->buf_queue(q, q->read_buf); 920 - spin_unlock_irqrestore(q->irqlock, flags); 921 - 922 - q->read_off = 0; 923 - } 924 - 925 - /* wait until capture is done */ 926 - retval = videobuf_waiton(q, q->read_buf, nonblocking, 1); 927 - if (0 != retval) 928 - goto done; 929 - 930 - CALL(q, sync, q, q->read_buf); 931 - 932 - if (VIDEOBUF_ERROR == q->read_buf->state) { 933 - /* catch I/O errors */ 934 - q->ops->buf_release(q, q->read_buf); 935 - kfree(q->read_buf); 936 - q->read_buf = NULL; 937 - retval = -EIO; 938 - goto done; 939 - } 940 - 941 - /* Copy to userspace */ 942 - retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking); 943 - if (retval < 0) 944 - goto done; 945 - 946 - q->read_off += retval; 947 - if (q->read_off == q->read_buf->size) { 948 - /* all data copied, cleanup */ 949 - q->ops->buf_release(q, q->read_buf); 950 - kfree(q->read_buf); 951 - q->read_buf = NULL; 952 - } 953 - 954 - done: 955 - videobuf_queue_unlock(q); 956 - return retval; 957 - } 958 - EXPORT_SYMBOL_GPL(videobuf_read_one); 959 - 960 - /* Locking: Caller holds q->vb_lock */ 961 - static int __videobuf_read_start(struct videobuf_queue *q) 962 - { 963 - enum v4l2_field field; 964 - unsigned long flags = 0; 965 - unsigned int count = 0, size = 0; 966 - int err, i; 967 - 968 - q->ops->buf_setup(q, &count, &size); 969 - if (count < 2) 970 - count = 2; 971 - if (count > VIDEO_MAX_FRAME) 972 - count = VIDEO_MAX_FRAME; 973 - size = PAGE_ALIGN(size); 974 - 975 - err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR); 976 - if (err < 0) 977 - return err; 978 - 979 - count = err; 980 - 981 - for (i = 0; i < count; i++) { 982 - field = videobuf_next_field(q); 983 - err = q->ops->buf_prepare(q, q->bufs[i], field); 984 - if (err) 985 - return err; 986 - list_add_tail(&q->bufs[i]->stream, &q->stream); 987 - } 988 - spin_lock_irqsave(q->irqlock, flags); 989 - for (i = 0; i < count; i++) 990 - q->ops->buf_queue(q, q->bufs[i]); 991 - spin_unlock_irqrestore(q->irqlock, flags); 992 - q->reading = 1; 993 - return 0; 994 - } 995 - 996 - static void __videobuf_read_stop(struct videobuf_queue *q) 997 - { 998 - int i; 999 - 1000 - videobuf_queue_cancel(q); 1001 - __videobuf_free(q); 1002 - INIT_LIST_HEAD(&q->stream); 1003 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 1004 - if (NULL == q->bufs[i]) 1005 - continue; 1006 - kfree(q->bufs[i]); 1007 - q->bufs[i] = NULL; 1008 - } 1009 - q->read_buf = NULL; 1010 - } 1011 - 1012 - int videobuf_read_start(struct videobuf_queue *q) 1013 - { 1014 - int rc; 1015 - 1016 - videobuf_queue_lock(q); 1017 - rc = __videobuf_read_start(q); 1018 - videobuf_queue_unlock(q); 1019 - 1020 - return rc; 1021 - } 1022 - EXPORT_SYMBOL_GPL(videobuf_read_start); 1023 - 1024 - void videobuf_read_stop(struct videobuf_queue *q) 1025 - { 1026 - videobuf_queue_lock(q); 1027 - __videobuf_read_stop(q); 1028 - videobuf_queue_unlock(q); 1029 - } 1030 - EXPORT_SYMBOL_GPL(videobuf_read_stop); 1031 - 1032 - void videobuf_stop(struct videobuf_queue *q) 1033 - { 1034 - videobuf_queue_lock(q); 1035 - 1036 - if (q->streaming) 1037 - __videobuf_streamoff(q); 1038 - 1039 - if (q->reading) 1040 - __videobuf_read_stop(q); 1041 - 1042 - videobuf_queue_unlock(q); 1043 - } 1044 - EXPORT_SYMBOL_GPL(videobuf_stop); 1045 - 1046 - ssize_t videobuf_read_stream(struct videobuf_queue *q, 1047 - char __user *data, size_t count, loff_t *ppos, 1048 - int vbihack, int nonblocking) 1049 - { 1050 - int rc, retval; 1051 - unsigned long flags = 0; 1052 - 1053 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 1054 - 1055 - dprintk(2, "%s\n", __func__); 1056 - videobuf_queue_lock(q); 1057 - retval = -EBUSY; 1058 - if (q->streaming) 1059 - goto done; 1060 - if (!q->reading) { 1061 - retval = __videobuf_read_start(q); 1062 - if (retval < 0) 1063 - goto done; 1064 - } 1065 - 1066 - retval = 0; 1067 - while (count > 0) { 1068 - /* get / wait for data */ 1069 - if (NULL == q->read_buf) { 1070 - q->read_buf = list_entry(q->stream.next, 1071 - struct videobuf_buffer, 1072 - stream); 1073 - list_del(&q->read_buf->stream); 1074 - q->read_off = 0; 1075 - } 1076 - rc = videobuf_waiton(q, q->read_buf, nonblocking, 1); 1077 - if (rc < 0) { 1078 - if (0 == retval) 1079 - retval = rc; 1080 - break; 1081 - } 1082 - 1083 - if (q->read_buf->state == VIDEOBUF_DONE) { 1084 - rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count, 1085 - retval, vbihack, nonblocking); 1086 - if (rc < 0) { 1087 - retval = rc; 1088 - break; 1089 - } 1090 - retval += rc; 1091 - count -= rc; 1092 - q->read_off += rc; 1093 - } else { 1094 - /* some error */ 1095 - q->read_off = q->read_buf->size; 1096 - if (0 == retval) 1097 - retval = -EIO; 1098 - } 1099 - 1100 - /* requeue buffer when done with copying */ 1101 - if (q->read_off == q->read_buf->size) { 1102 - list_add_tail(&q->read_buf->stream, 1103 - &q->stream); 1104 - spin_lock_irqsave(q->irqlock, flags); 1105 - q->ops->buf_queue(q, q->read_buf); 1106 - spin_unlock_irqrestore(q->irqlock, flags); 1107 - q->read_buf = NULL; 1108 - } 1109 - if (retval < 0) 1110 - break; 1111 - } 1112 - 1113 - done: 1114 - videobuf_queue_unlock(q); 1115 - return retval; 1116 - } 1117 - EXPORT_SYMBOL_GPL(videobuf_read_stream); 1118 - 1119 - __poll_t videobuf_poll_stream(struct file *file, 1120 - struct videobuf_queue *q, 1121 - poll_table *wait) 1122 - { 1123 - __poll_t req_events = poll_requested_events(wait); 1124 - struct videobuf_buffer *buf = NULL; 1125 - __poll_t rc = 0; 1126 - 1127 - videobuf_queue_lock(q); 1128 - if (q->streaming) { 1129 - if (!list_empty(&q->stream)) 1130 - buf = list_entry(q->stream.next, 1131 - struct videobuf_buffer, stream); 1132 - } else if (req_events & (EPOLLIN | EPOLLRDNORM)) { 1133 - if (!q->reading) 1134 - __videobuf_read_start(q); 1135 - if (!q->reading) { 1136 - rc = EPOLLERR; 1137 - } else if (NULL == q->read_buf) { 1138 - q->read_buf = list_entry(q->stream.next, 1139 - struct videobuf_buffer, 1140 - stream); 1141 - list_del(&q->read_buf->stream); 1142 - q->read_off = 0; 1143 - } 1144 - buf = q->read_buf; 1145 - } 1146 - if (buf) 1147 - poll_wait(file, &buf->done, wait); 1148 - else 1149 - rc = EPOLLERR; 1150 - 1151 - if (0 == rc) { 1152 - if (buf->state == VIDEOBUF_DONE || 1153 - buf->state == VIDEOBUF_ERROR) { 1154 - switch (q->type) { 1155 - case V4L2_BUF_TYPE_VIDEO_OUTPUT: 1156 - case V4L2_BUF_TYPE_VBI_OUTPUT: 1157 - case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: 1158 - case V4L2_BUF_TYPE_SDR_OUTPUT: 1159 - rc = EPOLLOUT | EPOLLWRNORM; 1160 - break; 1161 - default: 1162 - rc = EPOLLIN | EPOLLRDNORM; 1163 - break; 1164 - } 1165 - } 1166 - } 1167 - videobuf_queue_unlock(q); 1168 - return rc; 1169 - } 1170 - EXPORT_SYMBOL_GPL(videobuf_poll_stream); 1171 - 1172 - int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma) 1173 - { 1174 - int rc = -EINVAL; 1175 - int i; 1176 - 1177 - MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); 1178 - 1179 - if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) { 1180 - dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n"); 1181 - return -EINVAL; 1182 - } 1183 - 1184 - videobuf_queue_lock(q); 1185 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 1186 - struct videobuf_buffer *buf = q->bufs[i]; 1187 - 1188 - if (buf && buf->memory == V4L2_MEMORY_MMAP && 1189 - buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) { 1190 - rc = CALL(q, mmap_mapper, q, buf, vma); 1191 - break; 1192 - } 1193 - } 1194 - videobuf_queue_unlock(q); 1195 - 1196 - return rc; 1197 - } 1198 - EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
-402
drivers/media/v4l2-core/videobuf-dma-contig.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * helper functions for physically contiguous capture buffers 4 - * 5 - * The functions support hardware lacking scatter gather support 6 - * (i.e. the buffers must be linear in physical memory) 7 - * 8 - * Copyright (c) 2008 Magnus Damm 9 - * 10 - * Based on videobuf-vmalloc.c, 11 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 12 - */ 13 - 14 - #include <linux/init.h> 15 - #include <linux/module.h> 16 - #include <linux/mm.h> 17 - #include <linux/pagemap.h> 18 - #include <linux/dma-mapping.h> 19 - #include <linux/sched.h> 20 - #include <linux/slab.h> 21 - #include <media/videobuf-dma-contig.h> 22 - 23 - struct videobuf_dma_contig_memory { 24 - u32 magic; 25 - void *vaddr; 26 - dma_addr_t dma_handle; 27 - unsigned long size; 28 - }; 29 - 30 - #define MAGIC_DC_MEM 0x0733ac61 31 - #define MAGIC_CHECK(is, should) \ 32 - if (unlikely((is) != (should))) { \ 33 - pr_err("magic mismatch: %x expected %x\n", (is), (should)); \ 34 - BUG(); \ 35 - } 36 - 37 - static int __videobuf_dc_alloc(struct device *dev, 38 - struct videobuf_dma_contig_memory *mem, 39 - unsigned long size) 40 - { 41 - mem->size = size; 42 - mem->vaddr = dma_alloc_coherent(dev, mem->size, &mem->dma_handle, 43 - GFP_KERNEL); 44 - if (!mem->vaddr) { 45 - dev_err(dev, "memory alloc size %ld failed\n", mem->size); 46 - return -ENOMEM; 47 - } 48 - 49 - dev_dbg(dev, "dma mapped data is at %p (%ld)\n", mem->vaddr, mem->size); 50 - 51 - return 0; 52 - } 53 - 54 - static void __videobuf_dc_free(struct device *dev, 55 - struct videobuf_dma_contig_memory *mem) 56 - { 57 - dma_free_coherent(dev, mem->size, mem->vaddr, mem->dma_handle); 58 - 59 - mem->vaddr = NULL; 60 - } 61 - 62 - static void videobuf_vm_open(struct vm_area_struct *vma) 63 - { 64 - struct videobuf_mapping *map = vma->vm_private_data; 65 - 66 - dev_dbg(map->q->dev, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", 67 - map, map->count, vma->vm_start, vma->vm_end); 68 - 69 - map->count++; 70 - } 71 - 72 - static void videobuf_vm_close(struct vm_area_struct *vma) 73 - { 74 - struct videobuf_mapping *map = vma->vm_private_data; 75 - struct videobuf_queue *q = map->q; 76 - int i; 77 - 78 - dev_dbg(q->dev, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", 79 - map, map->count, vma->vm_start, vma->vm_end); 80 - 81 - map->count--; 82 - if (0 == map->count) { 83 - struct videobuf_dma_contig_memory *mem; 84 - 85 - dev_dbg(q->dev, "munmap %p q=%p\n", map, q); 86 - videobuf_queue_lock(q); 87 - 88 - /* We need first to cancel streams, before unmapping */ 89 - if (q->streaming) 90 - videobuf_queue_cancel(q); 91 - 92 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 93 - if (NULL == q->bufs[i]) 94 - continue; 95 - 96 - if (q->bufs[i]->map != map) 97 - continue; 98 - 99 - mem = q->bufs[i]->priv; 100 - if (mem) { 101 - /* This callback is called only if kernel has 102 - allocated memory and this memory is mmapped. 103 - In this case, memory should be freed, 104 - in order to do memory unmap. 105 - */ 106 - 107 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 108 - 109 - /* vfree is not atomic - can't be 110 - called with IRQ's disabled 111 - */ 112 - dev_dbg(q->dev, "buf[%d] freeing %p\n", 113 - i, mem->vaddr); 114 - 115 - __videobuf_dc_free(q->dev, mem); 116 - mem->vaddr = NULL; 117 - } 118 - 119 - q->bufs[i]->map = NULL; 120 - q->bufs[i]->baddr = 0; 121 - } 122 - 123 - kfree(map); 124 - 125 - videobuf_queue_unlock(q); 126 - } 127 - } 128 - 129 - static const struct vm_operations_struct videobuf_vm_ops = { 130 - .open = videobuf_vm_open, 131 - .close = videobuf_vm_close, 132 - }; 133 - 134 - /** 135 - * videobuf_dma_contig_user_put() - reset pointer to user space buffer 136 - * @mem: per-buffer private videobuf-dma-contig data 137 - * 138 - * This function resets the user space pointer 139 - */ 140 - static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem) 141 - { 142 - mem->dma_handle = 0; 143 - mem->size = 0; 144 - } 145 - 146 - /** 147 - * videobuf_dma_contig_user_get() - setup user space memory pointer 148 - * @mem: per-buffer private videobuf-dma-contig data 149 - * @vb: video buffer to map 150 - * 151 - * This function validates and sets up a pointer to user space memory. 152 - * Only physically contiguous pfn-mapped memory is accepted. 153 - * 154 - * Returns 0 if successful. 155 - */ 156 - static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem, 157 - struct videobuf_buffer *vb) 158 - { 159 - unsigned long untagged_baddr = untagged_addr(vb->baddr); 160 - struct mm_struct *mm = current->mm; 161 - struct vm_area_struct *vma; 162 - unsigned long prev_pfn, this_pfn; 163 - unsigned long pages_done, user_address; 164 - unsigned int offset; 165 - int ret; 166 - 167 - offset = untagged_baddr & ~PAGE_MASK; 168 - mem->size = PAGE_ALIGN(vb->size + offset); 169 - ret = -EINVAL; 170 - 171 - mmap_read_lock(mm); 172 - 173 - vma = find_vma(mm, untagged_baddr); 174 - if (!vma) 175 - goto out_up; 176 - 177 - if ((untagged_baddr + mem->size) > vma->vm_end) 178 - goto out_up; 179 - 180 - pages_done = 0; 181 - prev_pfn = 0; /* kill warning */ 182 - user_address = untagged_baddr; 183 - 184 - while (pages_done < (mem->size >> PAGE_SHIFT)) { 185 - ret = follow_pfn(vma, user_address, &this_pfn); 186 - if (ret) 187 - break; 188 - 189 - if (pages_done == 0) 190 - mem->dma_handle = (this_pfn << PAGE_SHIFT) + offset; 191 - else if (this_pfn != (prev_pfn + 1)) 192 - ret = -EFAULT; 193 - 194 - if (ret) 195 - break; 196 - 197 - prev_pfn = this_pfn; 198 - user_address += PAGE_SIZE; 199 - pages_done++; 200 - } 201 - 202 - out_up: 203 - mmap_read_unlock(current->mm); 204 - 205 - return ret; 206 - } 207 - 208 - static struct videobuf_buffer *__videobuf_alloc(size_t size) 209 - { 210 - struct videobuf_dma_contig_memory *mem; 211 - struct videobuf_buffer *vb; 212 - 213 - vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); 214 - if (vb) { 215 - vb->priv = ((char *)vb) + size; 216 - mem = vb->priv; 217 - mem->magic = MAGIC_DC_MEM; 218 - } 219 - 220 - return vb; 221 - } 222 - 223 - static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) 224 - { 225 - struct videobuf_dma_contig_memory *mem = buf->priv; 226 - 227 - BUG_ON(!mem); 228 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 229 - 230 - return mem->vaddr; 231 - } 232 - 233 - static int __videobuf_iolock(struct videobuf_queue *q, 234 - struct videobuf_buffer *vb, 235 - struct v4l2_framebuffer *fbuf) 236 - { 237 - struct videobuf_dma_contig_memory *mem = vb->priv; 238 - 239 - BUG_ON(!mem); 240 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 241 - 242 - switch (vb->memory) { 243 - case V4L2_MEMORY_MMAP: 244 - dev_dbg(q->dev, "%s memory method MMAP\n", __func__); 245 - 246 - /* All handling should be done by __videobuf_mmap_mapper() */ 247 - if (!mem->vaddr) { 248 - dev_err(q->dev, "memory is not allocated/mmapped.\n"); 249 - return -EINVAL; 250 - } 251 - break; 252 - case V4L2_MEMORY_USERPTR: 253 - dev_dbg(q->dev, "%s memory method USERPTR\n", __func__); 254 - 255 - /* handle pointer from user space */ 256 - if (vb->baddr) 257 - return videobuf_dma_contig_user_get(mem, vb); 258 - 259 - /* allocate memory for the read() method */ 260 - if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(vb->size))) 261 - return -ENOMEM; 262 - break; 263 - case V4L2_MEMORY_OVERLAY: 264 - default: 265 - dev_dbg(q->dev, "%s memory method OVERLAY/unknown\n", __func__); 266 - return -EINVAL; 267 - } 268 - 269 - return 0; 270 - } 271 - 272 - static int __videobuf_mmap_mapper(struct videobuf_queue *q, 273 - struct videobuf_buffer *buf, 274 - struct vm_area_struct *vma) 275 - { 276 - struct videobuf_dma_contig_memory *mem; 277 - struct videobuf_mapping *map; 278 - int retval; 279 - 280 - dev_dbg(q->dev, "%s\n", __func__); 281 - 282 - /* create mapping + update buffer list */ 283 - map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); 284 - if (!map) 285 - return -ENOMEM; 286 - 287 - buf->map = map; 288 - map->q = q; 289 - 290 - buf->baddr = vma->vm_start; 291 - 292 - mem = buf->priv; 293 - BUG_ON(!mem); 294 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 295 - 296 - if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(buf->bsize))) 297 - goto error; 298 - 299 - /* the "vm_pgoff" is just used in v4l2 to find the 300 - * corresponding buffer data structure which is allocated 301 - * earlier and it does not mean the offset from the physical 302 - * buffer start address as usual. So set it to 0 to pass 303 - * the sanity check in dma_mmap_coherent(). 304 - */ 305 - vma->vm_pgoff = 0; 306 - retval = dma_mmap_coherent(q->dev, vma, mem->vaddr, mem->dma_handle, 307 - mem->size); 308 - if (retval) { 309 - dev_err(q->dev, "mmap: remap failed with error %d. ", 310 - retval); 311 - dma_free_coherent(q->dev, mem->size, 312 - mem->vaddr, mem->dma_handle); 313 - goto error; 314 - } 315 - 316 - vma->vm_ops = &videobuf_vm_ops; 317 - vm_flags_set(vma, VM_DONTEXPAND); 318 - vma->vm_private_data = map; 319 - 320 - dev_dbg(q->dev, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", 321 - map, q, vma->vm_start, vma->vm_end, 322 - (long int)buf->bsize, vma->vm_pgoff, buf->i); 323 - 324 - videobuf_vm_open(vma); 325 - 326 - return 0; 327 - 328 - error: 329 - kfree(map); 330 - return -ENOMEM; 331 - } 332 - 333 - static struct videobuf_qtype_ops qops = { 334 - .magic = MAGIC_QTYPE_OPS, 335 - .alloc_vb = __videobuf_alloc, 336 - .iolock = __videobuf_iolock, 337 - .mmap_mapper = __videobuf_mmap_mapper, 338 - .vaddr = __videobuf_to_vaddr, 339 - }; 340 - 341 - void videobuf_queue_dma_contig_init(struct videobuf_queue *q, 342 - const struct videobuf_queue_ops *ops, 343 - struct device *dev, 344 - spinlock_t *irqlock, 345 - enum v4l2_buf_type type, 346 - enum v4l2_field field, 347 - unsigned int msize, 348 - void *priv, 349 - struct mutex *ext_lock) 350 - { 351 - videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, 352 - priv, &qops, ext_lock); 353 - } 354 - EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init); 355 - 356 - dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf) 357 - { 358 - struct videobuf_dma_contig_memory *mem = buf->priv; 359 - 360 - BUG_ON(!mem); 361 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 362 - 363 - return mem->dma_handle; 364 - } 365 - EXPORT_SYMBOL_GPL(videobuf_to_dma_contig); 366 - 367 - void videobuf_dma_contig_free(struct videobuf_queue *q, 368 - struct videobuf_buffer *buf) 369 - { 370 - struct videobuf_dma_contig_memory *mem = buf->priv; 371 - 372 - /* mmapped memory can't be freed here, otherwise mmapped region 373 - would be released, while still needed. In this case, the memory 374 - release should happen inside videobuf_vm_close(). 375 - So, it should free memory only if the memory were allocated for 376 - read() operation. 377 - */ 378 - if (buf->memory != V4L2_MEMORY_USERPTR) 379 - return; 380 - 381 - if (!mem) 382 - return; 383 - 384 - MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); 385 - 386 - /* handle user space pointer case */ 387 - if (buf->baddr) { 388 - videobuf_dma_contig_user_put(mem); 389 - return; 390 - } 391 - 392 - /* read() method */ 393 - if (mem->vaddr) { 394 - __videobuf_dc_free(q->dev, mem); 395 - mem->vaddr = NULL; 396 - } 397 - } 398 - EXPORT_SYMBOL_GPL(videobuf_dma_contig_free); 399 - 400 - MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers"); 401 - MODULE_AUTHOR("Magnus Damm"); 402 - MODULE_LICENSE("GPL");
-681
drivers/media/v4l2-core/videobuf-dma-sg.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * helper functions for SG DMA video4linux capture buffers 4 - * 5 - * The functions expect the hardware being able to scatter gather 6 - * (i.e. the buffers are not linear in physical memory, but fragmented 7 - * into PAGE_SIZE chunks). They also assume the driver does not need 8 - * to touch the video data. 9 - * 10 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 11 - * 12 - * Highly based on video-buf written originally by: 13 - * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> 14 - * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org> 15 - * (c) 2006 Ted Walther and John Sokol 16 - */ 17 - 18 - #include <linux/init.h> 19 - #include <linux/module.h> 20 - #include <linux/moduleparam.h> 21 - #include <linux/sched/mm.h> 22 - #include <linux/slab.h> 23 - #include <linux/interrupt.h> 24 - #include <linux/pgtable.h> 25 - 26 - #include <linux/dma-mapping.h> 27 - #include <linux/vmalloc.h> 28 - #include <linux/pagemap.h> 29 - #include <linux/scatterlist.h> 30 - #include <asm/page.h> 31 - 32 - #include <media/videobuf-dma-sg.h> 33 - 34 - #define MAGIC_DMABUF 0x19721112 35 - #define MAGIC_SG_MEM 0x17890714 36 - 37 - #define MAGIC_CHECK(is, should) \ 38 - if (unlikely((is) != (should))) { \ 39 - printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \ 40 - is, should); \ 41 - BUG(); \ 42 - } 43 - 44 - static int debug; 45 - module_param(debug, int, 0644); 46 - 47 - MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers"); 48 - MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>"); 49 - MODULE_LICENSE("GPL"); 50 - 51 - #define dprintk(level, fmt, arg...) \ 52 - if (debug >= level) \ 53 - printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) 54 - 55 - /* --------------------------------------------------------------------- */ 56 - 57 - /* 58 - * Return a scatterlist for some page-aligned vmalloc()'ed memory 59 - * block (NULL on errors). Memory for the scatterlist is allocated 60 - * using kmalloc. The caller must free the memory. 61 - */ 62 - static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt, 63 - int nr_pages) 64 - { 65 - struct scatterlist *sglist; 66 - struct page *pg; 67 - int i; 68 - 69 - sglist = vzalloc(array_size(nr_pages, sizeof(*sglist))); 70 - if (NULL == sglist) 71 - return NULL; 72 - sg_init_table(sglist, nr_pages); 73 - for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { 74 - pg = vmalloc_to_page(virt); 75 - if (NULL == pg) 76 - goto err; 77 - BUG_ON(PageHighMem(pg)); 78 - sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); 79 - } 80 - return sglist; 81 - 82 - err: 83 - vfree(sglist); 84 - return NULL; 85 - } 86 - 87 - /* 88 - * Return a scatterlist for a an array of userpages (NULL on errors). 89 - * Memory for the scatterlist is allocated using kmalloc. The caller 90 - * must free the memory. 91 - */ 92 - static struct scatterlist *videobuf_pages_to_sg(struct page **pages, 93 - int nr_pages, int offset, size_t size) 94 - { 95 - struct scatterlist *sglist; 96 - int i; 97 - 98 - if (NULL == pages[0]) 99 - return NULL; 100 - sglist = vmalloc(array_size(nr_pages, sizeof(*sglist))); 101 - if (NULL == sglist) 102 - return NULL; 103 - sg_init_table(sglist, nr_pages); 104 - 105 - if (PageHighMem(pages[0])) 106 - /* DMA to highmem pages might not work */ 107 - goto highmem; 108 - sg_set_page(&sglist[0], pages[0], 109 - min_t(size_t, PAGE_SIZE - offset, size), offset); 110 - size -= min_t(size_t, PAGE_SIZE - offset, size); 111 - for (i = 1; i < nr_pages; i++) { 112 - if (NULL == pages[i]) 113 - goto nopage; 114 - if (PageHighMem(pages[i])) 115 - goto highmem; 116 - sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0); 117 - size -= min_t(size_t, PAGE_SIZE, size); 118 - } 119 - return sglist; 120 - 121 - nopage: 122 - dprintk(2, "sgl: oops - no page\n"); 123 - vfree(sglist); 124 - return NULL; 125 - 126 - highmem: 127 - dprintk(2, "sgl: oops - highmem page\n"); 128 - vfree(sglist); 129 - return NULL; 130 - } 131 - 132 - /* --------------------------------------------------------------------- */ 133 - 134 - struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf) 135 - { 136 - struct videobuf_dma_sg_memory *mem = buf->priv; 137 - BUG_ON(!mem); 138 - 139 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 140 - 141 - return &mem->dma; 142 - } 143 - EXPORT_SYMBOL_GPL(videobuf_to_dma); 144 - 145 - static void videobuf_dma_init(struct videobuf_dmabuf *dma) 146 - { 147 - memset(dma, 0, sizeof(*dma)); 148 - dma->magic = MAGIC_DMABUF; 149 - } 150 - 151 - static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, 152 - int direction, unsigned long data, unsigned long size) 153 - { 154 - unsigned int gup_flags = FOLL_LONGTERM; 155 - unsigned long first, last; 156 - int err; 157 - 158 - dma->direction = direction; 159 - switch (dma->direction) { 160 - case DMA_FROM_DEVICE: 161 - gup_flags |= FOLL_WRITE; 162 - break; 163 - case DMA_TO_DEVICE: 164 - break; 165 - default: 166 - BUG(); 167 - } 168 - 169 - first = (data & PAGE_MASK) >> PAGE_SHIFT; 170 - last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; 171 - dma->offset = data & ~PAGE_MASK; 172 - dma->size = size; 173 - dma->nr_pages = last-first+1; 174 - dma->pages = kmalloc_array(dma->nr_pages, sizeof(struct page *), 175 - GFP_KERNEL); 176 - if (NULL == dma->pages) 177 - return -ENOMEM; 178 - 179 - dprintk(1, "init user [0x%lx+0x%lx => %lu pages]\n", 180 - data, size, dma->nr_pages); 181 - 182 - err = pin_user_pages(data & PAGE_MASK, dma->nr_pages, gup_flags, 183 - dma->pages); 184 - 185 - if (err != dma->nr_pages) { 186 - dma->nr_pages = (err >= 0) ? err : 0; 187 - dprintk(1, "pin_user_pages: err=%d [%lu]\n", err, 188 - dma->nr_pages); 189 - return err < 0 ? err : -EINVAL; 190 - } 191 - return 0; 192 - } 193 - 194 - static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, 195 - unsigned long data, unsigned long size) 196 - { 197 - int ret; 198 - 199 - mmap_read_lock(current->mm); 200 - ret = videobuf_dma_init_user_locked(dma, direction, data, size); 201 - mmap_read_unlock(current->mm); 202 - 203 - return ret; 204 - } 205 - 206 - static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, 207 - unsigned long nr_pages) 208 - { 209 - int i; 210 - 211 - dprintk(1, "init kernel [%lu pages]\n", nr_pages); 212 - 213 - dma->direction = direction; 214 - dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages), 215 - GFP_KERNEL); 216 - if (!dma->vaddr_pages) 217 - return -ENOMEM; 218 - 219 - dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL); 220 - if (!dma->dma_addr) { 221 - kfree(dma->vaddr_pages); 222 - return -ENOMEM; 223 - } 224 - for (i = 0; i < nr_pages; i++) { 225 - void *addr; 226 - 227 - addr = dma_alloc_coherent(dma->dev, PAGE_SIZE, 228 - &(dma->dma_addr[i]), GFP_KERNEL); 229 - if (addr == NULL) 230 - goto out_free_pages; 231 - 232 - dma->vaddr_pages[i] = virt_to_page(addr); 233 - } 234 - dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP, 235 - PAGE_KERNEL); 236 - if (NULL == dma->vaddr) { 237 - dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages); 238 - goto out_free_pages; 239 - } 240 - 241 - dprintk(1, "vmalloc is at addr %p, size=%lu\n", 242 - dma->vaddr, nr_pages << PAGE_SHIFT); 243 - 244 - memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT); 245 - dma->nr_pages = nr_pages; 246 - 247 - return 0; 248 - out_free_pages: 249 - while (i > 0) { 250 - void *addr; 251 - 252 - i--; 253 - addr = page_address(dma->vaddr_pages[i]); 254 - dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]); 255 - } 256 - kfree(dma->dma_addr); 257 - dma->dma_addr = NULL; 258 - kfree(dma->vaddr_pages); 259 - dma->vaddr_pages = NULL; 260 - 261 - return -ENOMEM; 262 - 263 - } 264 - 265 - static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, 266 - dma_addr_t addr, unsigned long nr_pages) 267 - { 268 - dprintk(1, "init overlay [%lu pages @ bus 0x%lx]\n", 269 - nr_pages, (unsigned long)addr); 270 - dma->direction = direction; 271 - 272 - if (0 == addr) 273 - return -EINVAL; 274 - 275 - dma->bus_addr = addr; 276 - dma->nr_pages = nr_pages; 277 - 278 - return 0; 279 - } 280 - 281 - static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma) 282 - { 283 - MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 284 - BUG_ON(0 == dma->nr_pages); 285 - 286 - if (dma->pages) { 287 - dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, 288 - dma->offset, dma->size); 289 - } 290 - if (dma->vaddr) { 291 - dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr, 292 - dma->nr_pages); 293 - } 294 - if (dma->bus_addr) { 295 - dma->sglist = vmalloc(sizeof(*dma->sglist)); 296 - if (NULL != dma->sglist) { 297 - dma->sglen = 1; 298 - sg_dma_address(&dma->sglist[0]) = dma->bus_addr 299 - & PAGE_MASK; 300 - dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; 301 - sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; 302 - } 303 - } 304 - if (NULL == dma->sglist) { 305 - dprintk(1, "scatterlist is NULL\n"); 306 - return -ENOMEM; 307 - } 308 - if (!dma->bus_addr) { 309 - dma->sglen = dma_map_sg(dev, dma->sglist, 310 - dma->nr_pages, dma->direction); 311 - if (0 == dma->sglen) { 312 - printk(KERN_WARNING 313 - "%s: videobuf_map_sg failed\n", __func__); 314 - vfree(dma->sglist); 315 - dma->sglist = NULL; 316 - dma->sglen = 0; 317 - return -ENOMEM; 318 - } 319 - } 320 - 321 - return 0; 322 - } 323 - 324 - int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma) 325 - { 326 - MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 327 - 328 - if (!dma->sglen) 329 - return 0; 330 - 331 - dma_unmap_sg(dev, dma->sglist, dma->nr_pages, dma->direction); 332 - 333 - vfree(dma->sglist); 334 - dma->sglist = NULL; 335 - dma->sglen = 0; 336 - 337 - return 0; 338 - } 339 - EXPORT_SYMBOL_GPL(videobuf_dma_unmap); 340 - 341 - int videobuf_dma_free(struct videobuf_dmabuf *dma) 342 - { 343 - int i; 344 - MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 345 - BUG_ON(dma->sglen); 346 - 347 - if (dma->pages) { 348 - unpin_user_pages_dirty_lock(dma->pages, dma->nr_pages, 349 - dma->direction == DMA_FROM_DEVICE); 350 - kfree(dma->pages); 351 - dma->pages = NULL; 352 - } 353 - 354 - if (dma->dma_addr) { 355 - for (i = 0; i < dma->nr_pages; i++) { 356 - void *addr; 357 - 358 - addr = page_address(dma->vaddr_pages[i]); 359 - dma_free_coherent(dma->dev, PAGE_SIZE, addr, 360 - dma->dma_addr[i]); 361 - } 362 - kfree(dma->dma_addr); 363 - dma->dma_addr = NULL; 364 - kfree(dma->vaddr_pages); 365 - dma->vaddr_pages = NULL; 366 - vunmap(dma->vaddr); 367 - dma->vaddr = NULL; 368 - } 369 - 370 - if (dma->bus_addr) 371 - dma->bus_addr = 0; 372 - dma->direction = DMA_NONE; 373 - 374 - return 0; 375 - } 376 - EXPORT_SYMBOL_GPL(videobuf_dma_free); 377 - 378 - /* --------------------------------------------------------------------- */ 379 - 380 - static void videobuf_vm_open(struct vm_area_struct *vma) 381 - { 382 - struct videobuf_mapping *map = vma->vm_private_data; 383 - 384 - dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map, 385 - map->count, vma->vm_start, vma->vm_end); 386 - 387 - map->count++; 388 - } 389 - 390 - static void videobuf_vm_close(struct vm_area_struct *vma) 391 - { 392 - struct videobuf_mapping *map = vma->vm_private_data; 393 - struct videobuf_queue *q = map->q; 394 - struct videobuf_dma_sg_memory *mem; 395 - int i; 396 - 397 - dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map, 398 - map->count, vma->vm_start, vma->vm_end); 399 - 400 - map->count--; 401 - if (0 == map->count) { 402 - dprintk(1, "munmap %p q=%p\n", map, q); 403 - videobuf_queue_lock(q); 404 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 405 - if (NULL == q->bufs[i]) 406 - continue; 407 - mem = q->bufs[i]->priv; 408 - if (!mem) 409 - continue; 410 - 411 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 412 - 413 - if (q->bufs[i]->map != map) 414 - continue; 415 - q->bufs[i]->map = NULL; 416 - q->bufs[i]->baddr = 0; 417 - q->ops->buf_release(q, q->bufs[i]); 418 - } 419 - videobuf_queue_unlock(q); 420 - kfree(map); 421 - } 422 - } 423 - 424 - /* 425 - * Get a anonymous page for the mapping. Make sure we can DMA to that 426 - * memory location with 32bit PCI devices (i.e. don't use highmem for 427 - * now ...). Bounce buffers don't work very well for the data rates 428 - * video capture has. 429 - */ 430 - static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf) 431 - { 432 - struct vm_area_struct *vma = vmf->vma; 433 - struct page *page; 434 - 435 - dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n", 436 - vmf->address, vma->vm_start, vma->vm_end); 437 - 438 - page = alloc_page(GFP_USER | __GFP_DMA32); 439 - if (!page) 440 - return VM_FAULT_OOM; 441 - clear_user_highpage(page, vmf->address); 442 - vmf->page = page; 443 - 444 - return 0; 445 - } 446 - 447 - static const struct vm_operations_struct videobuf_vm_ops = { 448 - .open = videobuf_vm_open, 449 - .close = videobuf_vm_close, 450 - .fault = videobuf_vm_fault, 451 - }; 452 - 453 - /* --------------------------------------------------------------------- 454 - * SG handlers for the generic methods 455 - */ 456 - 457 - /* Allocated area consists on 3 parts: 458 - struct video_buffer 459 - struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) 460 - struct videobuf_dma_sg_memory 461 - */ 462 - 463 - static struct videobuf_buffer *__videobuf_alloc_vb(size_t size) 464 - { 465 - struct videobuf_dma_sg_memory *mem; 466 - struct videobuf_buffer *vb; 467 - 468 - vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); 469 - if (!vb) 470 - return vb; 471 - 472 - mem = vb->priv = ((char *)vb) + size; 473 - mem->magic = MAGIC_SG_MEM; 474 - 475 - videobuf_dma_init(&mem->dma); 476 - 477 - dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n", 478 - __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb), 479 - mem, (long)sizeof(*mem)); 480 - 481 - return vb; 482 - } 483 - 484 - static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) 485 - { 486 - struct videobuf_dma_sg_memory *mem = buf->priv; 487 - BUG_ON(!mem); 488 - 489 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 490 - 491 - return mem->dma.vaddr; 492 - } 493 - 494 - static int __videobuf_iolock(struct videobuf_queue *q, 495 - struct videobuf_buffer *vb, 496 - struct v4l2_framebuffer *fbuf) 497 - { 498 - struct videobuf_dma_sg_memory *mem = vb->priv; 499 - unsigned long pages; 500 - dma_addr_t bus; 501 - int err; 502 - 503 - BUG_ON(!mem); 504 - 505 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 506 - 507 - if (!mem->dma.dev) 508 - mem->dma.dev = q->dev; 509 - else 510 - WARN_ON(mem->dma.dev != q->dev); 511 - 512 - switch (vb->memory) { 513 - case V4L2_MEMORY_MMAP: 514 - case V4L2_MEMORY_USERPTR: 515 - if (0 == vb->baddr) { 516 - /* no userspace addr -- kernel bounce buffer */ 517 - pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; 518 - err = videobuf_dma_init_kernel(&mem->dma, 519 - DMA_FROM_DEVICE, 520 - pages); 521 - if (0 != err) 522 - return err; 523 - } else if (vb->memory == V4L2_MEMORY_USERPTR) { 524 - /* dma directly to userspace */ 525 - err = videobuf_dma_init_user(&mem->dma, 526 - DMA_FROM_DEVICE, 527 - vb->baddr, vb->bsize); 528 - if (0 != err) 529 - return err; 530 - } else { 531 - /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP 532 - buffers can only be called from videobuf_qbuf 533 - we take current->mm->mmap_lock there, to prevent 534 - locking inversion, so don't take it here */ 535 - 536 - err = videobuf_dma_init_user_locked(&mem->dma, 537 - DMA_FROM_DEVICE, 538 - vb->baddr, vb->bsize); 539 - if (0 != err) 540 - return err; 541 - } 542 - break; 543 - case V4L2_MEMORY_OVERLAY: 544 - if (NULL == fbuf) 545 - return -EINVAL; 546 - /* FIXME: need sanity checks for vb->boff */ 547 - /* 548 - * Using a double cast to avoid compiler warnings when 549 - * building for PAE. Compiler doesn't like direct casting 550 - * of a 32 bit ptr to 64 bit integer. 551 - */ 552 - bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; 553 - pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; 554 - err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE, 555 - bus, pages); 556 - if (0 != err) 557 - return err; 558 - break; 559 - default: 560 - BUG(); 561 - } 562 - err = videobuf_dma_map(q->dev, &mem->dma); 563 - if (0 != err) 564 - return err; 565 - 566 - return 0; 567 - } 568 - 569 - static int __videobuf_sync(struct videobuf_queue *q, 570 - struct videobuf_buffer *buf) 571 - { 572 - struct videobuf_dma_sg_memory *mem = buf->priv; 573 - BUG_ON(!mem || !mem->dma.sglen); 574 - 575 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 576 - MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF); 577 - 578 - dma_sync_sg_for_cpu(q->dev, mem->dma.sglist, 579 - mem->dma.nr_pages, mem->dma.direction); 580 - 581 - return 0; 582 - } 583 - 584 - static int __videobuf_mmap_mapper(struct videobuf_queue *q, 585 - struct videobuf_buffer *buf, 586 - struct vm_area_struct *vma) 587 - { 588 - struct videobuf_dma_sg_memory *mem = buf->priv; 589 - struct videobuf_mapping *map; 590 - unsigned int first, last, size = 0, i; 591 - int retval; 592 - 593 - retval = -EINVAL; 594 - 595 - BUG_ON(!mem); 596 - MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 597 - 598 - /* look for first buffer to map */ 599 - for (first = 0; first < VIDEO_MAX_FRAME; first++) { 600 - if (buf == q->bufs[first]) { 601 - size = PAGE_ALIGN(q->bufs[first]->bsize); 602 - break; 603 - } 604 - } 605 - 606 - /* paranoia, should never happen since buf is always valid. */ 607 - if (!size) { 608 - dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n", 609 - (vma->vm_pgoff << PAGE_SHIFT)); 610 - goto done; 611 - } 612 - 613 - last = first; 614 - 615 - /* create mapping + update buffer list */ 616 - retval = -ENOMEM; 617 - map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); 618 - if (NULL == map) 619 - goto done; 620 - 621 - size = 0; 622 - for (i = first; i <= last; i++) { 623 - if (NULL == q->bufs[i]) 624 - continue; 625 - q->bufs[i]->map = map; 626 - q->bufs[i]->baddr = vma->vm_start + size; 627 - size += PAGE_ALIGN(q->bufs[i]->bsize); 628 - } 629 - 630 - map->count = 1; 631 - map->q = q; 632 - vma->vm_ops = &videobuf_vm_ops; 633 - /* using shared anonymous pages */ 634 - vm_flags_mod(vma, VM_DONTEXPAND | VM_DONTDUMP, VM_IO); 635 - vma->vm_private_data = map; 636 - dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", 637 - map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last); 638 - retval = 0; 639 - 640 - done: 641 - return retval; 642 - } 643 - 644 - static struct videobuf_qtype_ops sg_ops = { 645 - .magic = MAGIC_QTYPE_OPS, 646 - 647 - .alloc_vb = __videobuf_alloc_vb, 648 - .iolock = __videobuf_iolock, 649 - .sync = __videobuf_sync, 650 - .mmap_mapper = __videobuf_mmap_mapper, 651 - .vaddr = __videobuf_to_vaddr, 652 - }; 653 - 654 - void *videobuf_sg_alloc(size_t size) 655 - { 656 - struct videobuf_queue q; 657 - 658 - /* Required to make generic handler to call __videobuf_alloc */ 659 - q.int_ops = &sg_ops; 660 - 661 - q.msize = size; 662 - 663 - return videobuf_alloc_vb(&q); 664 - } 665 - EXPORT_SYMBOL_GPL(videobuf_sg_alloc); 666 - 667 - void videobuf_queue_sg_init(struct videobuf_queue *q, 668 - const struct videobuf_queue_ops *ops, 669 - struct device *dev, 670 - spinlock_t *irqlock, 671 - enum v4l2_buf_type type, 672 - enum v4l2_field field, 673 - unsigned int msize, 674 - void *priv, 675 - struct mutex *ext_lock) 676 - { 677 - videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, 678 - priv, &sg_ops, ext_lock); 679 - } 680 - EXPORT_SYMBOL_GPL(videobuf_queue_sg_init); 681 -
-326
drivers/media/v4l2-core/videobuf-vmalloc.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * helper functions for vmalloc video4linux capture buffers 4 - * 5 - * The functions expect the hardware being able to scatter gather 6 - * (i.e. the buffers are not linear in physical memory, but fragmented 7 - * into PAGE_SIZE chunks). They also assume the driver does not need 8 - * to touch the video data. 9 - * 10 - * (c) 2007 Mauro Carvalho Chehab <mchehab@kernel.org> 11 - */ 12 - 13 - #include <linux/init.h> 14 - #include <linux/module.h> 15 - #include <linux/moduleparam.h> 16 - #include <linux/slab.h> 17 - #include <linux/interrupt.h> 18 - #include <linux/pgtable.h> 19 - 20 - #include <linux/pci.h> 21 - #include <linux/vmalloc.h> 22 - #include <linux/pagemap.h> 23 - #include <asm/page.h> 24 - 25 - #include <media/videobuf-vmalloc.h> 26 - 27 - #define MAGIC_DMABUF 0x17760309 28 - #define MAGIC_VMAL_MEM 0x18221223 29 - 30 - #define MAGIC_CHECK(is, should) \ 31 - if (unlikely((is) != (should))) { \ 32 - printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \ 33 - is, should); \ 34 - BUG(); \ 35 - } 36 - 37 - static int debug; 38 - module_param(debug, int, 0644); 39 - 40 - MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers"); 41 - MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>"); 42 - MODULE_LICENSE("GPL"); 43 - 44 - #define dprintk(level, fmt, arg...) \ 45 - if (debug >= level) \ 46 - printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg) 47 - 48 - 49 - /***************************************************************************/ 50 - 51 - static void videobuf_vm_open(struct vm_area_struct *vma) 52 - { 53 - struct videobuf_mapping *map = vma->vm_private_data; 54 - 55 - dprintk(2, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", map, 56 - map->count, vma->vm_start, vma->vm_end); 57 - 58 - map->count++; 59 - } 60 - 61 - static void videobuf_vm_close(struct vm_area_struct *vma) 62 - { 63 - struct videobuf_mapping *map = vma->vm_private_data; 64 - struct videobuf_queue *q = map->q; 65 - int i; 66 - 67 - dprintk(2, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", map, 68 - map->count, vma->vm_start, vma->vm_end); 69 - 70 - map->count--; 71 - if (0 == map->count) { 72 - struct videobuf_vmalloc_memory *mem; 73 - 74 - dprintk(1, "munmap %p q=%p\n", map, q); 75 - videobuf_queue_lock(q); 76 - 77 - /* We need first to cancel streams, before unmapping */ 78 - if (q->streaming) 79 - videobuf_queue_cancel(q); 80 - 81 - for (i = 0; i < VIDEO_MAX_FRAME; i++) { 82 - if (NULL == q->bufs[i]) 83 - continue; 84 - 85 - if (q->bufs[i]->map != map) 86 - continue; 87 - 88 - mem = q->bufs[i]->priv; 89 - if (mem) { 90 - /* This callback is called only if kernel has 91 - allocated memory and this memory is mmapped. 92 - In this case, memory should be freed, 93 - in order to do memory unmap. 94 - */ 95 - 96 - MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); 97 - 98 - /* vfree is not atomic - can't be 99 - called with IRQ's disabled 100 - */ 101 - dprintk(1, "%s: buf[%d] freeing (%p)\n", 102 - __func__, i, mem->vaddr); 103 - 104 - vfree(mem->vaddr); 105 - mem->vaddr = NULL; 106 - } 107 - 108 - q->bufs[i]->map = NULL; 109 - q->bufs[i]->baddr = 0; 110 - } 111 - 112 - kfree(map); 113 - 114 - videobuf_queue_unlock(q); 115 - } 116 - 117 - return; 118 - } 119 - 120 - static const struct vm_operations_struct videobuf_vm_ops = { 121 - .open = videobuf_vm_open, 122 - .close = videobuf_vm_close, 123 - }; 124 - 125 - /* --------------------------------------------------------------------- 126 - * vmalloc handlers for the generic methods 127 - */ 128 - 129 - /* Allocated area consists on 3 parts: 130 - struct video_buffer 131 - struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) 132 - struct videobuf_dma_sg_memory 133 - */ 134 - 135 - static struct videobuf_buffer *__videobuf_alloc_vb(size_t size) 136 - { 137 - struct videobuf_vmalloc_memory *mem; 138 - struct videobuf_buffer *vb; 139 - 140 - vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); 141 - if (!vb) 142 - return vb; 143 - 144 - mem = vb->priv = ((char *)vb) + size; 145 - mem->magic = MAGIC_VMAL_MEM; 146 - 147 - dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n", 148 - __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb), 149 - mem, (long)sizeof(*mem)); 150 - 151 - return vb; 152 - } 153 - 154 - static int __videobuf_iolock(struct videobuf_queue *q, 155 - struct videobuf_buffer *vb, 156 - struct v4l2_framebuffer *fbuf) 157 - { 158 - struct videobuf_vmalloc_memory *mem = vb->priv; 159 - int pages; 160 - 161 - BUG_ON(!mem); 162 - 163 - MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); 164 - 165 - switch (vb->memory) { 166 - case V4L2_MEMORY_MMAP: 167 - dprintk(1, "%s memory method MMAP\n", __func__); 168 - 169 - /* All handling should be done by __videobuf_mmap_mapper() */ 170 - if (!mem->vaddr) { 171 - printk(KERN_ERR "memory is not allocated/mmapped.\n"); 172 - return -EINVAL; 173 - } 174 - break; 175 - case V4L2_MEMORY_USERPTR: 176 - pages = PAGE_ALIGN(vb->size); 177 - 178 - dprintk(1, "%s memory method USERPTR\n", __func__); 179 - 180 - if (vb->baddr) { 181 - printk(KERN_ERR "USERPTR is currently not supported\n"); 182 - return -EINVAL; 183 - } 184 - 185 - /* The only USERPTR currently supported is the one needed for 186 - * read() method. 187 - */ 188 - 189 - mem->vaddr = vmalloc_user(pages); 190 - if (!mem->vaddr) { 191 - printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); 192 - return -ENOMEM; 193 - } 194 - dprintk(1, "vmalloc is at addr %p (%d pages)\n", 195 - mem->vaddr, pages); 196 - break; 197 - case V4L2_MEMORY_OVERLAY: 198 - default: 199 - dprintk(1, "%s memory method OVERLAY/unknown\n", __func__); 200 - 201 - /* Currently, doesn't support V4L2_MEMORY_OVERLAY */ 202 - printk(KERN_ERR "Memory method currently unsupported.\n"); 203 - return -EINVAL; 204 - } 205 - 206 - return 0; 207 - } 208 - 209 - static int __videobuf_mmap_mapper(struct videobuf_queue *q, 210 - struct videobuf_buffer *buf, 211 - struct vm_area_struct *vma) 212 - { 213 - struct videobuf_vmalloc_memory *mem; 214 - struct videobuf_mapping *map; 215 - int retval, pages; 216 - 217 - dprintk(1, "%s\n", __func__); 218 - 219 - /* create mapping + update buffer list */ 220 - map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); 221 - if (NULL == map) 222 - return -ENOMEM; 223 - 224 - buf->map = map; 225 - map->q = q; 226 - 227 - buf->baddr = vma->vm_start; 228 - 229 - mem = buf->priv; 230 - BUG_ON(!mem); 231 - MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); 232 - 233 - pages = PAGE_ALIGN(vma->vm_end - vma->vm_start); 234 - mem->vaddr = vmalloc_user(pages); 235 - if (!mem->vaddr) { 236 - printk(KERN_ERR "vmalloc (%d pages) failed\n", pages); 237 - goto error; 238 - } 239 - dprintk(1, "vmalloc is at addr %p (%d pages)\n", mem->vaddr, pages); 240 - 241 - /* Try to remap memory */ 242 - retval = remap_vmalloc_range(vma, mem->vaddr, 0); 243 - if (retval < 0) { 244 - printk(KERN_ERR "mmap: remap failed with error %d. ", retval); 245 - vfree(mem->vaddr); 246 - goto error; 247 - } 248 - 249 - vma->vm_ops = &videobuf_vm_ops; 250 - vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 251 - vma->vm_private_data = map; 252 - 253 - dprintk(1, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", 254 - map, q, vma->vm_start, vma->vm_end, 255 - (long int)buf->bsize, 256 - vma->vm_pgoff, buf->i); 257 - 258 - videobuf_vm_open(vma); 259 - 260 - return 0; 261 - 262 - error: 263 - mem = NULL; 264 - kfree(map); 265 - return -ENOMEM; 266 - } 267 - 268 - static struct videobuf_qtype_ops qops = { 269 - .magic = MAGIC_QTYPE_OPS, 270 - 271 - .alloc_vb = __videobuf_alloc_vb, 272 - .iolock = __videobuf_iolock, 273 - .mmap_mapper = __videobuf_mmap_mapper, 274 - .vaddr = videobuf_to_vmalloc, 275 - }; 276 - 277 - void videobuf_queue_vmalloc_init(struct videobuf_queue *q, 278 - const struct videobuf_queue_ops *ops, 279 - struct device *dev, 280 - spinlock_t *irqlock, 281 - enum v4l2_buf_type type, 282 - enum v4l2_field field, 283 - unsigned int msize, 284 - void *priv, 285 - struct mutex *ext_lock) 286 - { 287 - videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, 288 - priv, &qops, ext_lock); 289 - } 290 - EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init); 291 - 292 - void *videobuf_to_vmalloc(struct videobuf_buffer *buf) 293 - { 294 - struct videobuf_vmalloc_memory *mem = buf->priv; 295 - BUG_ON(!mem); 296 - MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); 297 - 298 - return mem->vaddr; 299 - } 300 - EXPORT_SYMBOL_GPL(videobuf_to_vmalloc); 301 - 302 - void videobuf_vmalloc_free(struct videobuf_buffer *buf) 303 - { 304 - struct videobuf_vmalloc_memory *mem = buf->priv; 305 - 306 - /* mmapped memory can't be freed here, otherwise mmapped region 307 - would be released, while still needed. In this case, the memory 308 - release should happen inside videobuf_vm_close(). 309 - So, it should free memory only if the memory were allocated for 310 - read() operation. 311 - */ 312 - if ((buf->memory != V4L2_MEMORY_USERPTR) || buf->baddr) 313 - return; 314 - 315 - if (!mem) 316 - return; 317 - 318 - MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM); 319 - 320 - vfree(mem->vaddr); 321 - mem->vaddr = NULL; 322 - 323 - return; 324 - } 325 - EXPORT_SYMBOL_GPL(videobuf_vmalloc_free); 326 -
-233
include/media/videobuf-core.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * generic helper functions for handling video4linux capture buffers 4 - * 5 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 6 - * 7 - * Highly based on video-buf written originally by: 8 - * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> 9 - * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org> 10 - * (c) 2006 Ted Walther and John Sokol 11 - */ 12 - 13 - #ifndef _VIDEOBUF_CORE_H 14 - #define _VIDEOBUF_CORE_H 15 - 16 - #include <linux/poll.h> 17 - #include <linux/videodev2.h> 18 - 19 - #define UNSET (-1U) 20 - 21 - 22 - struct videobuf_buffer; 23 - struct videobuf_queue; 24 - 25 - /* --------------------------------------------------------------------- */ 26 - 27 - /* 28 - * A small set of helper functions to manage video4linux buffers. 29 - * 30 - * struct videobuf_buffer holds the data structures used by the helper 31 - * functions, additionally some commonly used fields for v4l buffers 32 - * (width, height, lists, waitqueue) are in there. That struct should 33 - * be used as first element in the drivers buffer struct. 34 - * 35 - * about the mmap helpers (videobuf_mmap_*): 36 - * 37 - * The mmaper function allows to map any subset of contiguous buffers. 38 - * This includes one mmap() call for all buffers (which the original 39 - * video4linux API uses) as well as one mmap() for every single buffer 40 - * (which v4l2 uses). 41 - * 42 - * If there is a valid mapping for a buffer, buffer->baddr/bsize holds 43 - * userspace address + size which can be fed into the 44 - * videobuf_dma_init_user function listed above. 45 - * 46 - */ 47 - 48 - struct videobuf_mapping { 49 - unsigned int count; 50 - struct videobuf_queue *q; 51 - }; 52 - 53 - enum videobuf_state { 54 - VIDEOBUF_NEEDS_INIT = 0, 55 - VIDEOBUF_PREPARED = 1, 56 - VIDEOBUF_QUEUED = 2, 57 - VIDEOBUF_ACTIVE = 3, 58 - VIDEOBUF_DONE = 4, 59 - VIDEOBUF_ERROR = 5, 60 - VIDEOBUF_IDLE = 6, 61 - }; 62 - 63 - struct videobuf_buffer { 64 - unsigned int i; 65 - u32 magic; 66 - 67 - /* info about the buffer */ 68 - unsigned int width; 69 - unsigned int height; 70 - unsigned int bytesperline; /* use only if != 0 */ 71 - unsigned long size; 72 - enum v4l2_field field; 73 - enum videobuf_state state; 74 - struct list_head stream; /* QBUF/DQBUF list */ 75 - 76 - /* touched by irq handler */ 77 - struct list_head queue; 78 - wait_queue_head_t done; 79 - unsigned int field_count; 80 - u64 ts; 81 - 82 - /* Memory type */ 83 - enum v4l2_memory memory; 84 - 85 - /* buffer size */ 86 - size_t bsize; 87 - 88 - /* buffer offset (mmap + overlay) */ 89 - size_t boff; 90 - 91 - /* buffer addr (userland ptr!) */ 92 - unsigned long baddr; 93 - 94 - /* for mmap'ed buffers */ 95 - struct videobuf_mapping *map; 96 - 97 - /* Private pointer to allow specific methods to store their data */ 98 - int privsize; 99 - void *priv; 100 - }; 101 - 102 - struct videobuf_queue_ops { 103 - int (*buf_setup)(struct videobuf_queue *q, 104 - unsigned int *count, unsigned int *size); 105 - int (*buf_prepare)(struct videobuf_queue *q, 106 - struct videobuf_buffer *vb, 107 - enum v4l2_field field); 108 - void (*buf_queue)(struct videobuf_queue *q, 109 - struct videobuf_buffer *vb); 110 - void (*buf_release)(struct videobuf_queue *q, 111 - struct videobuf_buffer *vb); 112 - }; 113 - 114 - #define MAGIC_QTYPE_OPS 0x12261003 115 - 116 - /* Helper operations - device type dependent */ 117 - struct videobuf_qtype_ops { 118 - u32 magic; 119 - 120 - struct videobuf_buffer *(*alloc_vb)(size_t size); 121 - void *(*vaddr) (struct videobuf_buffer *buf); 122 - int (*iolock) (struct videobuf_queue *q, 123 - struct videobuf_buffer *vb, 124 - struct v4l2_framebuffer *fbuf); 125 - int (*sync) (struct videobuf_queue *q, 126 - struct videobuf_buffer *buf); 127 - int (*mmap_mapper) (struct videobuf_queue *q, 128 - struct videobuf_buffer *buf, 129 - struct vm_area_struct *vma); 130 - }; 131 - 132 - struct videobuf_queue { 133 - struct mutex vb_lock; 134 - struct mutex *ext_lock; 135 - spinlock_t *irqlock; 136 - struct device *dev; 137 - 138 - wait_queue_head_t wait; /* wait if queue is empty */ 139 - 140 - enum v4l2_buf_type type; 141 - unsigned int msize; 142 - enum v4l2_field field; 143 - enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */ 144 - struct videobuf_buffer *bufs[VIDEO_MAX_FRAME]; 145 - const struct videobuf_queue_ops *ops; 146 - struct videobuf_qtype_ops *int_ops; 147 - 148 - unsigned int streaming:1; 149 - unsigned int reading:1; 150 - 151 - /* capture via mmap() + ioctl(QBUF/DQBUF) */ 152 - struct list_head stream; 153 - 154 - /* capture via read() */ 155 - unsigned int read_off; 156 - struct videobuf_buffer *read_buf; 157 - 158 - /* driver private data */ 159 - void *priv_data; 160 - }; 161 - 162 - static inline void videobuf_queue_lock(struct videobuf_queue *q) 163 - { 164 - if (!q->ext_lock) 165 - mutex_lock(&q->vb_lock); 166 - } 167 - 168 - static inline void videobuf_queue_unlock(struct videobuf_queue *q) 169 - { 170 - if (!q->ext_lock) 171 - mutex_unlock(&q->vb_lock); 172 - } 173 - 174 - int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb, 175 - int non_blocking, int intr); 176 - int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, 177 - struct v4l2_framebuffer *fbuf); 178 - 179 - struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q); 180 - 181 - /* Used on videobuf-dvb */ 182 - void *videobuf_queue_to_vaddr(struct videobuf_queue *q, 183 - struct videobuf_buffer *buf); 184 - 185 - void videobuf_queue_core_init(struct videobuf_queue *q, 186 - const struct videobuf_queue_ops *ops, 187 - struct device *dev, 188 - spinlock_t *irqlock, 189 - enum v4l2_buf_type type, 190 - enum v4l2_field field, 191 - unsigned int msize, 192 - void *priv, 193 - struct videobuf_qtype_ops *int_ops, 194 - struct mutex *ext_lock); 195 - int videobuf_queue_is_busy(struct videobuf_queue *q); 196 - void videobuf_queue_cancel(struct videobuf_queue *q); 197 - 198 - enum v4l2_field videobuf_next_field(struct videobuf_queue *q); 199 - int videobuf_reqbufs(struct videobuf_queue *q, 200 - struct v4l2_requestbuffers *req); 201 - int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b); 202 - int videobuf_qbuf(struct videobuf_queue *q, 203 - struct v4l2_buffer *b); 204 - int videobuf_dqbuf(struct videobuf_queue *q, 205 - struct v4l2_buffer *b, int nonblocking); 206 - int videobuf_streamon(struct videobuf_queue *q); 207 - int videobuf_streamoff(struct videobuf_queue *q); 208 - 209 - void videobuf_stop(struct videobuf_queue *q); 210 - 211 - int videobuf_read_start(struct videobuf_queue *q); 212 - void videobuf_read_stop(struct videobuf_queue *q); 213 - ssize_t videobuf_read_stream(struct videobuf_queue *q, 214 - char __user *data, size_t count, loff_t *ppos, 215 - int vbihack, int nonblocking); 216 - ssize_t videobuf_read_one(struct videobuf_queue *q, 217 - char __user *data, size_t count, loff_t *ppos, 218 - int nonblocking); 219 - __poll_t videobuf_poll_stream(struct file *file, 220 - struct videobuf_queue *q, 221 - poll_table *wait); 222 - 223 - int videobuf_mmap_setup(struct videobuf_queue *q, 224 - unsigned int bcount, unsigned int bsize, 225 - enum v4l2_memory memory); 226 - int __videobuf_mmap_setup(struct videobuf_queue *q, 227 - unsigned int bcount, unsigned int bsize, 228 - enum v4l2_memory memory); 229 - int videobuf_mmap_free(struct videobuf_queue *q); 230 - int videobuf_mmap_mapper(struct videobuf_queue *q, 231 - struct vm_area_struct *vma); 232 - 233 - #endif
-30
include/media/videobuf-dma-contig.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * helper functions for physically contiguous capture buffers 4 - * 5 - * The functions support hardware lacking scatter gather support 6 - * (i.e. the buffers must be linear in physical memory) 7 - * 8 - * Copyright (c) 2008 Magnus Damm 9 - */ 10 - #ifndef _VIDEOBUF_DMA_CONTIG_H 11 - #define _VIDEOBUF_DMA_CONTIG_H 12 - 13 - #include <linux/dma-mapping.h> 14 - #include <media/videobuf-core.h> 15 - 16 - void videobuf_queue_dma_contig_init(struct videobuf_queue *q, 17 - const struct videobuf_queue_ops *ops, 18 - struct device *dev, 19 - spinlock_t *irqlock, 20 - enum v4l2_buf_type type, 21 - enum v4l2_field field, 22 - unsigned int msize, 23 - void *priv, 24 - struct mutex *ext_lock); 25 - 26 - dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf); 27 - void videobuf_dma_contig_free(struct videobuf_queue *q, 28 - struct videobuf_buffer *buf); 29 - 30 - #endif /* _VIDEOBUF_DMA_CONTIG_H */
-102
include/media/videobuf-dma-sg.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * helper functions for SG DMA video4linux capture buffers 4 - * 5 - * The functions expect the hardware being able to scatter gather 6 - * (i.e. the buffers are not linear in physical memory, but fragmented 7 - * into PAGE_SIZE chunks). They also assume the driver does not need 8 - * to touch the video data. 9 - * 10 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 11 - * 12 - * Highly based on video-buf written originally by: 13 - * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> 14 - * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org> 15 - * (c) 2006 Ted Walther and John Sokol 16 - */ 17 - #ifndef _VIDEOBUF_DMA_SG_H 18 - #define _VIDEOBUF_DMA_SG_H 19 - 20 - #include <media/videobuf-core.h> 21 - 22 - /* --------------------------------------------------------------------- */ 23 - 24 - /* 25 - * A small set of helper functions to manage buffers (both userland 26 - * and kernel) for DMA. 27 - * 28 - * videobuf_dma_init_*() 29 - * creates a buffer. The userland version takes a userspace 30 - * pointer + length. The kernel version just wants the size and 31 - * does memory allocation too using vmalloc_32(). 32 - * 33 - * videobuf_dma_*() 34 - * see Documentation/core-api/dma-api-howto.rst, these functions to 35 - * basically the same. The map function does also build a 36 - * scatterlist for the buffer (and unmap frees it ...) 37 - * 38 - * videobuf_dma_free() 39 - * no comment ... 40 - * 41 - */ 42 - 43 - struct videobuf_dmabuf { 44 - u32 magic; 45 - 46 - /* for userland buffer */ 47 - int offset; 48 - size_t size; 49 - struct page **pages; 50 - 51 - /* for kernel buffers */ 52 - void *vaddr; 53 - struct page **vaddr_pages; 54 - dma_addr_t *dma_addr; 55 - struct device *dev; 56 - 57 - /* for overlay buffers (pci-pci dma) */ 58 - dma_addr_t bus_addr; 59 - 60 - /* common */ 61 - struct scatterlist *sglist; 62 - int sglen; 63 - unsigned long nr_pages; 64 - int direction; 65 - }; 66 - 67 - struct videobuf_dma_sg_memory { 68 - u32 magic; 69 - 70 - /* for mmap'ed buffers */ 71 - struct videobuf_dmabuf dma; 72 - }; 73 - 74 - /* 75 - * Scatter-gather DMA buffer API. 76 - * 77 - * These functions provide a simple way to create a page list and a 78 - * scatter-gather list from a kernel, userspace of physical address and map the 79 - * memory for DMA operation. 80 - * 81 - * Despite the name, this is totally unrelated to videobuf, except that 82 - * videobuf-dma-sg uses the same API internally. 83 - */ 84 - int videobuf_dma_free(struct videobuf_dmabuf *dma); 85 - 86 - int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma); 87 - struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf); 88 - 89 - void *videobuf_sg_alloc(size_t size); 90 - 91 - void videobuf_queue_sg_init(struct videobuf_queue *q, 92 - const struct videobuf_queue_ops *ops, 93 - struct device *dev, 94 - spinlock_t *irqlock, 95 - enum v4l2_buf_type type, 96 - enum v4l2_field field, 97 - unsigned int msize, 98 - void *priv, 99 - struct mutex *ext_lock); 100 - 101 - #endif /* _VIDEOBUF_DMA_SG_H */ 102 -
-43
include/media/videobuf-vmalloc.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-only */ 2 - /* 3 - * helper functions for vmalloc capture buffers 4 - * 5 - * The functions expect the hardware being able to scatter gather 6 - * (i.e. the buffers are not linear in physical memory, but fragmented 7 - * into PAGE_SIZE chunks). They also assume the driver does not need 8 - * to touch the video data. 9 - * 10 - * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 11 - */ 12 - #ifndef _VIDEOBUF_VMALLOC_H 13 - #define _VIDEOBUF_VMALLOC_H 14 - 15 - #include <media/videobuf-core.h> 16 - 17 - /* --------------------------------------------------------------------- */ 18 - 19 - struct videobuf_vmalloc_memory { 20 - u32 magic; 21 - 22 - void *vaddr; 23 - 24 - /* remap_vmalloc_range seems to need to run 25 - * after mmap() on some cases */ 26 - struct vm_area_struct *vma; 27 - }; 28 - 29 - void videobuf_queue_vmalloc_init(struct videobuf_queue *q, 30 - const struct videobuf_queue_ops *ops, 31 - struct device *dev, 32 - spinlock_t *irqlock, 33 - enum v4l2_buf_type type, 34 - enum v4l2_field field, 35 - unsigned int msize, 36 - void *priv, 37 - struct mutex *ext_lock); 38 - 39 - void *videobuf_to_vmalloc(struct videobuf_buffer *buf); 40 - 41 - void videobuf_vmalloc_free(struct videobuf_buffer *buf); 42 - 43 - #endif