Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[deliverable/linux.git] / drivers / media / video / videobuf-vmalloc.c
CommitLineData
87b9ad07
MCC
1/*
2 * helper functions for vmalloc video4linux capture buffers
3 *
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.
8 *
9 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
10 *
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
14 */
15
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>
21
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>
27
28#include <media/videobuf-vmalloc.h>
29
30#define MAGIC_DMABUF 0x17760309
31#define MAGIC_VMAL_MEM 0x18221223
32
33#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
34 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
35
36static int debug = 0;
37module_param(debug, int, 0644);
38
39MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
40MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
41MODULE_LICENSE("GPL");
42
43#define dprintk(level, fmt, arg...) if (debug >= level) \
44 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
45
46
47/***************************************************************************/
48
49static void
50videobuf_vm_open(struct vm_area_struct *vma)
51{
52 struct videobuf_mapping *map = vma->vm_private_data;
53
54 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
55 map->count,vma->vm_start,vma->vm_end);
56
57 map->count++;
58}
59
60static void
61videobuf_vm_close(struct vm_area_struct *vma)
62{
63 struct videobuf_mapping *map = vma->vm_private_data;
64 struct videobuf_queue *q = map->q;
87b9ad07
MCC
65 int i;
66
67 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
68 map->count,vma->vm_start,vma->vm_end);
69
70 map->count--;
71 if (0 == map->count) {
72 dprintk(1,"munmap %p q=%p\n",map,q);
73 mutex_lock(&q->lock);
74 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
75 if (NULL == q->bufs[i])
76 continue;
87b9ad07 77
851c0c96 78 if (q->bufs[i]->map != map)
87b9ad07 79 continue;
123f8ef6
MCC
80
81 q->ops->buf_release(q,q->bufs[i]);
82
851c0c96 83 q->bufs[i]->map = NULL;
87b9ad07 84 q->bufs[i]->baddr = 0;
87b9ad07
MCC
85 }
86 mutex_unlock(&q->lock);
87 kfree(map);
88 }
89 return;
90}
91
92static struct vm_operations_struct videobuf_vm_ops =
93{
94 .open = videobuf_vm_open,
95 .close = videobuf_vm_close,
96};
97
98/* ---------------------------------------------------------------------
99 * vmalloc handlers for the generic methods
100 */
101
102/* Allocated area consists on 3 parts:
103 struct video_buffer
104 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
105 struct videobuf_pci_sg_memory
106 */
107
108static void *__videobuf_alloc(size_t size)
109{
110 struct videbuf_vmalloc_memory *mem;
111 struct videobuf_buffer *vb;
112
113 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
114
115 mem = vb->priv = ((char *)vb)+size;
116 mem->magic=MAGIC_VMAL_MEM;
117
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));
121
122 return vb;
123}
124
125static int __videobuf_iolock (struct videobuf_queue* q,
126 struct videobuf_buffer *vb,
127 struct v4l2_framebuffer *fbuf)
128{
129 int pages;
130
131 struct videbuf_vmalloc_memory *mem=vb->priv;
132
133
134 BUG_ON(!mem);
135
136 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
137
87b9ad07
MCC
138 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
139
140 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
141 if ((vb->memory != V4L2_MEMORY_MMAP) &&
142 (vb->memory != V4L2_MEMORY_USERPTR) ) {
143 printk(KERN_ERR "Method currently unsupported.\n");
144 return -EINVAL;
145 }
146
147 /* FIXME: should be tested with kernel mmap mem */
148 mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
149 if (NULL == mem->vmalloc) {
e78dcf55 150 printk(KERN_ERR "vmalloc (%d pages) failed\n",pages);
87b9ad07
MCC
151 return -ENOMEM;
152 }
153
154 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
155 (unsigned long)mem->vmalloc,
156 pages << PAGE_SHIFT);
157
158 /* It seems that some kernel versions need to do remap *after*
159 the mmap() call
160 */
161 if (mem->vma) {
162 int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
163 kfree(mem->vma);
164 mem->vma=NULL;
165 if (retval<0) {
166 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
167 mem->vmalloc,retval);
168 return retval;
169 }
170 }
171
172 return 0;
173}
174
175static int __videobuf_sync(struct videobuf_queue *q,
176 struct videobuf_buffer *buf)
177{
178 return 0;
179}
180
181static int __videobuf_mmap_free(struct videobuf_queue *q)
182{
183 unsigned int i;
184
185 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
186 if (q->bufs[i]) {
851c0c96 187 if (q->bufs[i]->map)
87b9ad07
MCC
188 return -EBUSY;
189 }
190 }
191
192 return 0;
193}
194
195static int __videobuf_mmap_mapper(struct videobuf_queue *q,
196 struct vm_area_struct *vma)
197{
198 struct videbuf_vmalloc_memory *mem;
199 struct videobuf_mapping *map;
200 unsigned int first;
201 int retval;
202 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
203
204 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
205 return -EINVAL;
206
207 /* look for first buffer to map */
208 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
209 if (NULL == q->bufs[first])
210 continue;
211
212 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
213 continue;
214 if (q->bufs[first]->boff == offset)
215 break;
216 }
217 if (VIDEO_MAX_FRAME == first) {
218 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
219 (vma->vm_pgoff << PAGE_SHIFT));
220 return -EINVAL;
221 }
87b9ad07
MCC
222
223 /* create mapping + update buffer list */
851c0c96 224 map = q->bufs[first]->map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
87b9ad07
MCC
225 if (NULL == map)
226 return -ENOMEM;
227
228 map->start = vma->vm_start;
229 map->end = vma->vm_end;
230 map->q = q;
231
232 q->bufs[first]->baddr = vma->vm_start;
233
234 vma->vm_ops = &videobuf_vm_ops;
235 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
236 vma->vm_private_data = map;
237
851c0c96
MCC
238 mem=q->bufs[first]->priv;
239 BUG_ON (!mem);
240 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
241
87b9ad07
MCC
242 /* Try to remap memory */
243 retval=remap_vmalloc_range(vma, mem->vmalloc,0);
244 if (retval<0) {
245 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
851c0c96 246
87b9ad07
MCC
247 mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
248 if (!mem->vma) {
249 kfree(map);
851c0c96 250 q->bufs[first]->map=NULL;
87b9ad07
MCC
251 return -ENOMEM;
252 }
253 memcpy(mem->vma,vma,sizeof(*vma));
254 }
255
256 dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
257 map,q,vma->vm_start,vma->vm_end,
258 (long int) q->bufs[first]->bsize,
259 vma->vm_pgoff,first);
260
261 videobuf_vm_open(vma);
262
263 return (0);
264}
265
87b9ad07
MCC
266static int __videobuf_copy_to_user ( struct videobuf_queue *q,
267 char __user *data, size_t count,
268 int nonblocking )
269{
270 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
271 BUG_ON (!mem);
272 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
273
274 BUG_ON (!mem->vmalloc);
275
276 /* copy to userspace */
277 if (count > q->read_buf->size - q->read_off)
278 count = q->read_buf->size - q->read_off;
279
280 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
281 return -EFAULT;
282
283 return count;
284}
285
286static int __videobuf_copy_stream ( struct videobuf_queue *q,
287 char __user *data, size_t count, size_t pos,
288 int vbihack, int nonblocking )
289{
290 unsigned int *fc;
291 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
292 BUG_ON (!mem);
293 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
294
295 if (vbihack) {
296 /* dirty, undocumented hack -- pass the frame counter
297 * within the last four bytes of each vbi data block.
298 * We need that one to maintain backward compatibility
299 * to all vbi decoding software out there ... */
300 fc = (unsigned int*)mem->vmalloc;
301 fc += (q->read_buf->size>>2) -1;
302 *fc = q->read_buf->field_count >> 1;
303 dprintk(1,"vbihack: %d\n",*fc);
304 }
305
306 /* copy stuff using the common method */
307 count = __videobuf_copy_to_user (q,data,count,nonblocking);
308
309 if ( (count==-EFAULT) && (0 == pos) )
310 return -EFAULT;
311
312 return count;
313}
314
315static struct videobuf_qtype_ops qops = {
316 .magic = MAGIC_QTYPE_OPS,
317
318 .alloc = __videobuf_alloc,
319 .iolock = __videobuf_iolock,
320 .sync = __videobuf_sync,
321 .mmap_free = __videobuf_mmap_free,
322 .mmap_mapper = __videobuf_mmap_mapper,
13bcd5d0 323 .video_copy_to_user = __videobuf_copy_to_user,
87b9ad07
MCC
324 .copy_stream = __videobuf_copy_stream,
325};
326
327void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
328 struct videobuf_queue_ops *ops,
329 void *dev,
330 spinlock_t *irqlock,
331 enum v4l2_buf_type type,
332 enum v4l2_field field,
333 unsigned int msize,
334 void *priv)
335{
d4cae5a5
MCC
336 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
337 priv, &qops);
87b9ad07
MCC
338}
339
340EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
341
342void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
343{
344 struct videbuf_vmalloc_memory *mem=buf->priv;
345 BUG_ON (!mem);
346 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
347
348 return mem->vmalloc;
349}
350EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
351
352void videobuf_vmalloc_free (struct videobuf_buffer *buf)
353{
354 struct videbuf_vmalloc_memory *mem=buf->priv;
355 BUG_ON (!mem);
356
357 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
358
359 vfree(mem->vmalloc);
c520a497 360 mem->vmalloc=NULL;
87b9ad07
MCC
361
362 return;
363}
364EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
365
366/*
367 * Local variables:
368 * c-basic-offset: 8
369 * End:
370 */
This page took 0.044061 seconds and 5 git commands to generate.