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-vmalloc: " 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=%u,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
;
67 dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map
,
68 map
->count
,vma
->vm_start
,vma
->vm_end
);
71 if (0 == map
->count
) {
72 dprintk(1,"munmap %p q=%p\n",map
,q
);
73 mutex_lock(&q
->vb_lock
);
74 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
75 if (NULL
== q
->bufs
[i
])
78 if (q
->bufs
[i
]->map
!= map
)
81 q
->ops
->buf_release(q
,q
->bufs
[i
]);
83 q
->bufs
[i
]->map
= NULL
;
84 q
->bufs
[i
]->baddr
= 0;
86 mutex_unlock(&q
->vb_lock
);
92 static struct vm_operations_struct videobuf_vm_ops
=
94 .open
= videobuf_vm_open
,
95 .close
= videobuf_vm_close
,
98 /* ---------------------------------------------------------------------
99 * vmalloc handlers for the generic methods
102 /* Allocated area consists on 3 parts:
104 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
105 struct videobuf_pci_sg_memory
108 static void *__videobuf_alloc(size_t size
)
110 struct videobuf_vmalloc_memory
*mem
;
111 struct videobuf_buffer
*vb
;
113 vb
= kzalloc(size
+sizeof(*mem
),GFP_KERNEL
);
115 mem
= vb
->priv
= ((char *)vb
)+size
;
116 mem
->magic
=MAGIC_VMAL_MEM
;
118 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
119 __FUNCTION__
,vb
,(long)sizeof(*vb
),(long)size
-sizeof(*vb
),
120 mem
,(long)sizeof(*mem
));
125 static int __videobuf_iolock (struct videobuf_queue
* q
,
126 struct videobuf_buffer
*vb
,
127 struct v4l2_framebuffer
*fbuf
)
130 struct videobuf_vmalloc_memory
*mem
=vb
->priv
;
134 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
136 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
138 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
139 if ((vb
->memory
!= V4L2_MEMORY_MMAP
) &&
140 (vb
->memory
!= V4L2_MEMORY_USERPTR
) ) {
141 printk(KERN_ERR
"Method currently unsupported.\n");
145 /* FIXME: should be tested with kernel mmap mem */
146 mem
->vmalloc
=vmalloc_user (PAGE_ALIGN(vb
->size
));
147 if (NULL
== mem
->vmalloc
) {
148 printk(KERN_ERR
"vmalloc (%d pages) failed\n",pages
);
152 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
153 (unsigned long)mem
->vmalloc
,
154 pages
<< PAGE_SHIFT
);
156 /* It seems that some kernel versions need to do remap *after*
160 int retval
=remap_vmalloc_range(mem
->vma
, mem
->vmalloc
,0);
164 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
165 mem
->vmalloc
,retval
);
173 static int __videobuf_sync(struct videobuf_queue
*q
,
174 struct videobuf_buffer
*buf
)
179 static int __videobuf_mmap_free(struct videobuf_queue
*q
)
183 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
193 static int __videobuf_mmap_mapper(struct videobuf_queue
*q
,
194 struct vm_area_struct
*vma
)
196 struct videobuf_vmalloc_memory
*mem
;
197 struct videobuf_mapping
*map
;
200 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
202 if (! (vma
->vm_flags
& VM_WRITE
) || ! (vma
->vm_flags
& VM_SHARED
))
205 /* look for first buffer to map */
206 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
207 if (NULL
== q
->bufs
[first
])
210 if (V4L2_MEMORY_MMAP
!= q
->bufs
[first
]->memory
)
212 if (q
->bufs
[first
]->boff
== offset
)
215 if (VIDEO_MAX_FRAME
== first
) {
216 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
217 (vma
->vm_pgoff
<< PAGE_SHIFT
));
221 /* create mapping + update buffer list */
222 map
= q
->bufs
[first
]->map
= kzalloc(sizeof(struct videobuf_mapping
),GFP_KERNEL
);
226 map
->start
= vma
->vm_start
;
227 map
->end
= vma
->vm_end
;
230 q
->bufs
[first
]->baddr
= vma
->vm_start
;
232 vma
->vm_ops
= &videobuf_vm_ops
;
233 vma
->vm_flags
|= VM_DONTEXPAND
| VM_RESERVED
;
234 vma
->vm_private_data
= map
;
236 mem
=q
->bufs
[first
]->priv
;
238 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
240 /* Try to remap memory */
241 retval
=remap_vmalloc_range(vma
, mem
->vmalloc
,0);
243 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
245 mem
->vma
=kmalloc(sizeof(*vma
),GFP_KERNEL
);
248 q
->bufs
[first
]->map
=NULL
;
251 memcpy(mem
->vma
,vma
,sizeof(*vma
));
254 dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
255 map
,q
,vma
->vm_start
,vma
->vm_end
,
256 (long int) q
->bufs
[first
]->bsize
,
257 vma
->vm_pgoff
,first
);
259 videobuf_vm_open(vma
);
264 static int __videobuf_copy_to_user ( struct videobuf_queue
*q
,
265 char __user
*data
, size_t count
,
268 struct videobuf_vmalloc_memory
*mem
=q
->read_buf
->priv
;
270 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
272 BUG_ON (!mem
->vmalloc
);
274 /* copy to userspace */
275 if (count
> q
->read_buf
->size
- q
->read_off
)
276 count
= q
->read_buf
->size
- q
->read_off
;
278 if (copy_to_user(data
, mem
->vmalloc
+q
->read_off
, count
))
284 static int __videobuf_copy_stream ( struct videobuf_queue
*q
,
285 char __user
*data
, size_t count
, size_t pos
,
286 int vbihack
, int nonblocking
)
289 struct videobuf_vmalloc_memory
*mem
=q
->read_buf
->priv
;
291 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
294 /* dirty, undocumented hack -- pass the frame counter
295 * within the last four bytes of each vbi data block.
296 * We need that one to maintain backward compatibility
297 * to all vbi decoding software out there ... */
298 fc
= (unsigned int*)mem
->vmalloc
;
299 fc
+= (q
->read_buf
->size
>>2) -1;
300 *fc
= q
->read_buf
->field_count
>> 1;
301 dprintk(1,"vbihack: %d\n",*fc
);
304 /* copy stuff using the common method */
305 count
= __videobuf_copy_to_user (q
,data
,count
,nonblocking
);
307 if ( (count
==-EFAULT
) && (0 == pos
) )
313 static struct videobuf_qtype_ops qops
= {
314 .magic
= MAGIC_QTYPE_OPS
,
316 .alloc
= __videobuf_alloc
,
317 .iolock
= __videobuf_iolock
,
318 .sync
= __videobuf_sync
,
319 .mmap_free
= __videobuf_mmap_free
,
320 .mmap_mapper
= __videobuf_mmap_mapper
,
321 .video_copy_to_user
= __videobuf_copy_to_user
,
322 .copy_stream
= __videobuf_copy_stream
,
325 void videobuf_queue_vmalloc_init(struct videobuf_queue
* q
,
326 struct videobuf_queue_ops
*ops
,
329 enum v4l2_buf_type type
,
330 enum v4l2_field field
,
334 videobuf_queue_core_init(q
, ops
, dev
, irqlock
, type
, field
, msize
,
338 EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init
);
340 void *videobuf_to_vmalloc (struct videobuf_buffer
*buf
)
342 struct videobuf_vmalloc_memory
*mem
=buf
->priv
;
344 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
348 EXPORT_SYMBOL_GPL(videobuf_to_vmalloc
);
350 void videobuf_vmalloc_free (struct videobuf_buffer
*buf
)
352 struct videobuf_vmalloc_memory
*mem
=buf
->priv
;
355 MAGIC_CHECK(mem
->magic
,MAGIC_VMAL_MEM
);
362 EXPORT_SYMBOL_GPL(videobuf_vmalloc_free
);