WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf-vmalloc.c
blob9b2443720ab017c34873c53b29c7dceeb8406196
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * helper functions for vmalloc video4linux capture buffers
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.
10 * (c) 2007 Mauro Carvalho Chehab <mchehab@kernel.org>
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>
20 #include <linux/pci.h>
21 #include <linux/vmalloc.h>
22 #include <linux/pagemap.h>
23 #include <asm/page.h>
25 #include <media/videobuf-vmalloc.h>
27 #define MAGIC_DMABUF 0x17760309
28 #define MAGIC_VMAL_MEM 0x18221223
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(); \
37 static int debug;
38 module_param(debug, int, 0644);
40 MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
41 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
42 MODULE_LICENSE("GPL");
44 #define dprintk(level, fmt, arg...) \
45 if (debug >= level) \
46 printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
49 /***************************************************************************/
51 static void videobuf_vm_open(struct vm_area_struct *vma)
53 struct videobuf_mapping *map = vma->vm_private_data;
55 dprintk(2, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", map,
56 map->count, vma->vm_start, vma->vm_end);
58 map->count++;
61 static void videobuf_vm_close(struct vm_area_struct *vma)
63 struct videobuf_mapping *map = vma->vm_private_data;
64 struct videobuf_queue *q = map->q;
65 int i;
67 dprintk(2, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", map,
68 map->count, vma->vm_start, vma->vm_end);
70 map->count--;
71 if (0 == map->count) {
72 struct videobuf_vmalloc_memory *mem;
74 dprintk(1, "munmap %p q=%p\n", map, q);
75 videobuf_queue_lock(q);
77 /* We need first to cancel streams, before unmapping */
78 if (q->streaming)
79 videobuf_queue_cancel(q);
81 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
82 if (NULL == q->bufs[i])
83 continue;
85 if (q->bufs[i]->map != map)
86 continue;
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.
96 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
98 /* vfree is not atomic - can't be
99 called with IRQ's disabled
101 dprintk(1, "%s: buf[%d] freeing (%p)\n",
102 __func__, i, mem->vaddr);
104 vfree(mem->vaddr);
105 mem->vaddr = NULL;
108 q->bufs[i]->map = NULL;
109 q->bufs[i]->baddr = 0;
112 kfree(map);
114 videobuf_queue_unlock(q);
117 return;
120 static const struct vm_operations_struct videobuf_vm_ops = {
121 .open = videobuf_vm_open,
122 .close = videobuf_vm_close,
125 /* ---------------------------------------------------------------------
126 * vmalloc handlers for the generic methods
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
135 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
137 struct videobuf_vmalloc_memory *mem;
138 struct videobuf_buffer *vb;
140 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
141 if (!vb)
142 return vb;
144 mem = vb->priv = ((char *)vb) + size;
145 mem->magic = MAGIC_VMAL_MEM;
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));
151 return vb;
154 static int __videobuf_iolock(struct videobuf_queue *q,
155 struct videobuf_buffer *vb,
156 struct v4l2_framebuffer *fbuf)
158 struct videobuf_vmalloc_memory *mem = vb->priv;
159 int pages;
161 BUG_ON(!mem);
163 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
165 switch (vb->memory) {
166 case V4L2_MEMORY_MMAP:
167 dprintk(1, "%s memory method MMAP\n", __func__);
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;
174 break;
175 case V4L2_MEMORY_USERPTR:
176 pages = PAGE_ALIGN(vb->size);
178 dprintk(1, "%s memory method USERPTR\n", __func__);
180 if (vb->baddr) {
181 printk(KERN_ERR "USERPTR is currently not supported\n");
182 return -EINVAL;
185 /* The only USERPTR currently supported is the one needed for
186 * read() method.
189 mem->vaddr = vmalloc_user(pages);
190 if (!mem->vaddr) {
191 printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
192 return -ENOMEM;
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__);
201 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
202 printk(KERN_ERR "Memory method currently unsupported.\n");
203 return -EINVAL;
206 return 0;
209 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
210 struct videobuf_buffer *buf,
211 struct vm_area_struct *vma)
213 struct videobuf_vmalloc_memory *mem;
214 struct videobuf_mapping *map;
215 int retval, pages;
217 dprintk(1, "%s\n", __func__);
219 /* create mapping + update buffer list */
220 map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
221 if (NULL == map)
222 return -ENOMEM;
224 buf->map = map;
225 map->q = q;
227 buf->baddr = vma->vm_start;
229 mem = buf->priv;
230 BUG_ON(!mem);
231 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
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;
239 dprintk(1, "vmalloc is at addr %p (%d pages)\n", mem->vaddr, pages);
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;
249 vma->vm_ops = &videobuf_vm_ops;
250 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
251 vma->vm_private_data = map;
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);
258 videobuf_vm_open(vma);
260 return 0;
262 error:
263 mem = NULL;
264 kfree(map);
265 return -ENOMEM;
268 static struct videobuf_qtype_ops qops = {
269 .magic = MAGIC_QTYPE_OPS,
271 .alloc_vb = __videobuf_alloc_vb,
272 .iolock = __videobuf_iolock,
273 .mmap_mapper = __videobuf_mmap_mapper,
274 .vaddr = videobuf_to_vmalloc,
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)
287 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
288 priv, &qops, ext_lock);
290 EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
292 void *videobuf_to_vmalloc(struct videobuf_buffer *buf)
294 struct videobuf_vmalloc_memory *mem = buf->priv;
295 BUG_ON(!mem);
296 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
298 return mem->vaddr;
300 EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
302 void videobuf_vmalloc_free(struct videobuf_buffer *buf)
304 struct videobuf_vmalloc_memory *mem = buf->priv;
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.
312 if ((buf->memory != V4L2_MEMORY_USERPTR) || buf->baddr)
313 return;
315 if (!mem)
316 return;
318 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
320 vfree(mem->vaddr);
321 mem->vaddr = NULL;
323 return;
325 EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);