V4L/DVB (6262): An allocation error message were being printed as a debug msg
[linux-2.6/mini2440.git] / drivers / media / video / videobuf-vmalloc.c
blobb2abfc9d9a8e68a34c1ce2a09b0665c8045536f6
1 /*
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>
25 #include <asm/page.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(); }
36 static int debug = 0;
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 /***************************************************************************/
49 static void
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);
57 map->count++;
60 static void
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;
66 int i;
68 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
69 map->count,vma->vm_start,vma->vm_end);
71 map->count--;
72 if (0 == map->count) {
73 dprintk(1,"munmap %p q=%p\n",map,q);
74 mutex_lock(&q->lock);
75 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
76 if (NULL == q->bufs[i])
77 continue;
78 mem=q->bufs[i]->priv;
80 if (!mem)
81 continue;
83 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
85 if (mem->map != map)
86 continue;
87 mem->map = NULL;
88 q->bufs[i]->baddr = 0;
89 q->ops->buf_release(q,q->bufs[i]);
91 mutex_unlock(&q->lock);
92 kfree(map);
94 return;
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:
108 struct video_buffer
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));
127 return vb;
130 static int __videobuf_iolock (struct videobuf_queue* q,
131 struct videobuf_buffer *vb,
132 struct v4l2_framebuffer *fbuf)
134 int pages;
136 struct videbuf_vmalloc_memory *mem=vb->priv;
139 BUG_ON(!mem);
141 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
143 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
145 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
146 if ((vb->memory != V4L2_MEMORY_MMAP) &&
147 (vb->memory != V4L2_MEMORY_USERPTR) ) {
148 printk(KERN_ERR "Method currently unsupported.\n");
149 return -EINVAL;
152 /* FIXME: should be tested with kernel mmap mem */
153 mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
154 if (NULL == mem->vmalloc) {
155 printk(KERN_ERR "vmalloc (%d pages) failed\n",pages);
156 return -ENOMEM;
159 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
160 (unsigned long)mem->vmalloc,
161 pages << PAGE_SHIFT);
163 /* It seems that some kernel versions need to do remap *after*
164 the mmap() call
166 if (mem->vma) {
167 int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
168 kfree(mem->vma);
169 mem->vma=NULL;
170 if (retval<0) {
171 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
172 mem->vmalloc,retval);
173 return retval;
177 return 0;
180 static int __videobuf_sync(struct videobuf_queue *q,
181 struct videobuf_buffer *buf)
183 return 0;
186 static int __videobuf_mmap_free(struct videobuf_queue *q)
188 unsigned int i;
190 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
191 if (q->bufs[i]) {
192 struct videbuf_vmalloc_memory *mem=q->bufs[i]->priv;
193 if (mem && mem->map)
194 return -EBUSY;
198 return 0;
201 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
202 struct vm_area_struct *vma)
204 struct videbuf_vmalloc_memory *mem;
205 struct videobuf_mapping *map;
206 unsigned int first;
207 int retval;
208 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
210 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
211 return -EINVAL;
213 /* look for first buffer to map */
214 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
215 if (NULL == q->bufs[first])
216 continue;
218 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
219 continue;
220 if (q->bufs[first]->boff == offset)
221 break;
223 if (VIDEO_MAX_FRAME == first) {
224 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
225 (vma->vm_pgoff << PAGE_SHIFT));
226 return -EINVAL;
228 mem=q->bufs[first]->priv;
229 BUG_ON (!mem);
230 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
232 /* create mapping + update buffer list */
233 map = mem->map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
234 if (NULL == map)
235 return -ENOMEM;
237 map->start = vma->vm_start;
238 map->end = vma->vm_end;
239 map->q = q;
241 q->bufs[first]->baddr = vma->vm_start;
243 vma->vm_ops = &videobuf_vm_ops;
244 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
245 vma->vm_private_data = map;
247 /* Try to remap memory */
248 retval=remap_vmalloc_range(vma, mem->vmalloc,0);
249 if (retval<0) {
250 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
251 mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
252 if (!mem->vma) {
253 kfree(map);
254 mem->map=NULL;
255 return -ENOMEM;
257 memcpy(mem->vma,vma,sizeof(*vma));
260 dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
261 map,q,vma->vm_start,vma->vm_end,
262 (long int) q->bufs[first]->bsize,
263 vma->vm_pgoff,first);
265 videobuf_vm_open(vma);
267 return (0);
270 static int __videobuf_is_mmapped (struct videobuf_buffer *buf)
272 struct videbuf_vmalloc_memory *mem=buf->priv;
273 BUG_ON (!mem);
274 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
276 return (mem->map)?1:0;
279 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
280 char __user *data, size_t count,
281 int nonblocking )
283 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
284 BUG_ON (!mem);
285 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
287 BUG_ON (!mem->vmalloc);
289 /* copy to userspace */
290 if (count > q->read_buf->size - q->read_off)
291 count = q->read_buf->size - q->read_off;
293 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
294 return -EFAULT;
296 return count;
299 static int __videobuf_copy_stream ( struct videobuf_queue *q,
300 char __user *data, size_t count, size_t pos,
301 int vbihack, int nonblocking )
303 unsigned int *fc;
304 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
305 BUG_ON (!mem);
306 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
308 if (vbihack) {
309 /* dirty, undocumented hack -- pass the frame counter
310 * within the last four bytes of each vbi data block.
311 * We need that one to maintain backward compatibility
312 * to all vbi decoding software out there ... */
313 fc = (unsigned int*)mem->vmalloc;
314 fc += (q->read_buf->size>>2) -1;
315 *fc = q->read_buf->field_count >> 1;
316 dprintk(1,"vbihack: %d\n",*fc);
319 /* copy stuff using the common method */
320 count = __videobuf_copy_to_user (q,data,count,nonblocking);
322 if ( (count==-EFAULT) && (0 == pos) )
323 return -EFAULT;
325 return count;
328 static struct videobuf_qtype_ops qops = {
329 .magic = MAGIC_QTYPE_OPS,
331 .alloc = __videobuf_alloc,
332 .iolock = __videobuf_iolock,
333 .sync = __videobuf_sync,
334 .mmap_free = __videobuf_mmap_free,
335 .mmap_mapper = __videobuf_mmap_mapper,
336 .is_mmapped = __videobuf_is_mmapped,
337 .copy_to_user = __videobuf_copy_to_user,
338 .copy_stream = __videobuf_copy_stream,
341 void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
342 struct videobuf_queue_ops *ops,
343 void *dev,
344 spinlock_t *irqlock,
345 enum v4l2_buf_type type,
346 enum v4l2_field field,
347 unsigned int msize,
348 void *priv)
350 videobuf_queue_init(q, ops, dev, irqlock, type, field, msize, priv);
351 q->int_ops=&qops;
354 EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
356 void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
358 struct videbuf_vmalloc_memory *mem=buf->priv;
359 BUG_ON (!mem);
360 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
362 return mem->vmalloc;
364 EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
366 void videobuf_vmalloc_free (struct videobuf_buffer *buf)
368 struct videbuf_vmalloc_memory *mem=buf->priv;
369 BUG_ON (!mem);
371 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
373 vfree(mem->vmalloc);
374 mem->vmalloc=NULL;
376 return;
378 EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
381 * Local variables:
382 * c-basic-offset: 8
383 * End: