2 * helper functions for vmalloc video4linux capture buffers
4 * The functions expect the hardware being able to scatter gatter
5 * (i.e. the buffers are not linear in physical memory, but fragmented
6 * into PAGE_SIZE chunks). They also assume the driver does not need
7 * to touch the video data.
9 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/vmalloc.h>
24 #include <linux/pagemap.h>
26 #include <asm/pgtable.h>
28 #include <media/videobuf-vmalloc.h>
30 #define MAGIC_DMABUF 0x17760309
31 #define MAGIC_VMAL_MEM 0x18221223
33 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
34 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
37 module_param(debug, int, 0644);
39 MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
40 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
41 MODULE_LICENSE("GPL");
43 #define dprintk(level, fmt, arg...) if (debug >= level) \
44 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
47 /***************************************************************************/
50 videobuf_vm_open(struct vm_area_struct *vma)
52 struct videobuf_mapping *map = vma->vm_private_data;
54 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
55 map->count,vma->vm_start,vma->vm_end);
61 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 struct videbuf_vmalloc_memory *mem;
68 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
69 map->count,vma->vm_start,vma->vm_end);
72 if (0 == map->count) {
73 dprintk(1,"munmap %p q=%p\n",map,q);
75 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
76 if (NULL == q->bufs[i])
83 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
88 q->bufs[i]->baddr = 0;
89 q->ops->buf_release(q,q->bufs[i]);
91 mutex_unlock(&q->lock);
97 static struct vm_operations_struct videobuf_vm_ops =
99 .open = videobuf_vm_open,
100 .close = videobuf_vm_close,
103 /* ---------------------------------------------------------------------
104 * vmalloc handlers for the generic methods
107 /* Allocated area consists on 3 parts:
109 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
110 struct videobuf_pci_sg_memory
113 static void *__videobuf_alloc(size_t size)
115 struct videbuf_vmalloc_memory *mem;
116 struct videobuf_buffer *vb;
118 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
120 mem = vb->priv = ((char *)vb)+size;
121 mem->magic=MAGIC_VMAL_MEM;
123 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
124 __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
125 mem,(long)sizeof(*mem));
130 static int __videobuf_iolock (struct videobuf_queue* q,
131 struct videobuf_buffer *vb,
132 struct v4l2_framebuffer *fbuf)
136 struct videbuf_vmalloc_memory *mem=vb->priv;
141 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
144 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
146 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
147 if ((vb->memory != V4L2_MEMORY_MMAP) &&
148 (vb->memory != V4L2_MEMORY_USERPTR) ) {
149 printk(KERN_ERR "Method currently unsupported.\n");
153 /* FIXME: should be tested with kernel mmap mem */
154 mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
155 if (NULL == mem->vmalloc) {
156 dprintk(1,"vmalloc (%d pages) failed\n",pages);
160 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
161 (unsigned long)mem->vmalloc,
162 pages << PAGE_SHIFT);
164 /* It seems that some kernel versions need to do remap *after*
168 int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
172 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
173 mem->vmalloc,retval);
181 static int __videobuf_sync(struct videobuf_queue *q,
182 struct videobuf_buffer *buf)
187 static int __videobuf_mmap_free(struct videobuf_queue *q)
191 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
193 struct videbuf_vmalloc_memory *mem=q->bufs[i]->priv;
202 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
203 struct vm_area_struct *vma)
205 struct videbuf_vmalloc_memory *mem;
206 struct videobuf_mapping *map;
209 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
211 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
214 /* look for first buffer to map */
215 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
216 if (NULL == q->bufs[first])
219 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
221 if (q->bufs[first]->boff == offset)
224 if (VIDEO_MAX_FRAME == first) {
225 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
226 (vma->vm_pgoff << PAGE_SHIFT));
229 mem=q->bufs[first]->priv;
231 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
233 /* create mapping + update buffer list */
234 map = mem->map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
238 map->start = vma->vm_start;
239 map->end = vma->vm_end;
242 q->bufs[first]->baddr = vma->vm_start;
244 vma->vm_ops = &videobuf_vm_ops;
245 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
246 vma->vm_private_data = map;
248 /* Try to remap memory */
249 retval=remap_vmalloc_range(vma, mem->vmalloc,0);
251 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
252 mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
258 memcpy(mem->vma,vma,sizeof(*vma));
261 dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
262 map,q,vma->vm_start,vma->vm_end,
263 (long int) q->bufs[first]->bsize,
264 vma->vm_pgoff,first);
266 videobuf_vm_open(vma);
271 static int __videobuf_is_mmapped (struct videobuf_buffer *buf)
273 struct videbuf_vmalloc_memory *mem=buf->priv;
275 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
277 return (mem->map)?1:0;
280 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
281 char __user *data, size_t count,
284 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
286 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
288 BUG_ON (!mem->vmalloc);
290 /* copy to userspace */
291 if (count > q->read_buf->size - q->read_off)
292 count = q->read_buf->size - q->read_off;
294 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
300 static int __videobuf_copy_stream ( struct videobuf_queue *q,
301 char __user *data, size_t count, size_t pos,
302 int vbihack, int nonblocking )
305 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
307 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
310 /* dirty, undocumented hack -- pass the frame counter
311 * within the last four bytes of each vbi data block.
312 * We need that one to maintain backward compatibility
313 * to all vbi decoding software out there ... */
314 fc = (unsigned int*)mem->vmalloc;
315 fc += (q->read_buf->size>>2) -1;
316 *fc = q->read_buf->field_count >> 1;
317 dprintk(1,"vbihack: %d\n",*fc);
320 /* copy stuff using the common method */
321 count = __videobuf_copy_to_user (q,data,count,nonblocking);
323 if ( (count==-EFAULT) && (0 == pos) )
329 static struct videobuf_qtype_ops qops = {
330 .magic = MAGIC_QTYPE_OPS,
332 .alloc = __videobuf_alloc,
333 .iolock = __videobuf_iolock,
334 .sync = __videobuf_sync,
335 .mmap_free = __videobuf_mmap_free,
336 .mmap_mapper = __videobuf_mmap_mapper,
337 .is_mmapped = __videobuf_is_mmapped,
338 .copy_to_user = __videobuf_copy_to_user,
339 .copy_stream = __videobuf_copy_stream,
342 void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
343 struct videobuf_queue_ops *ops,
346 enum v4l2_buf_type type,
347 enum v4l2_field field,
351 videobuf_queue_init(q, ops, dev, irqlock, type, field, msize, priv);
355 EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
357 void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
359 struct videbuf_vmalloc_memory *mem=buf->priv;
361 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
365 EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
367 void videobuf_vmalloc_free (struct videobuf_buffer *buf)
369 struct videbuf_vmalloc_memory *mem=buf->priv;
372 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
379 EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);