aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@infradead.org>2007-08-02 22:31:33 -0400
committerMauro Carvalho Chehab <mchehab@infradead.org>2007-10-09 21:14:56 -0400
commit87b9ad070cf76c0f1e8cf836f7eb86e9ac94e34a (patch)
treeaeda5c7b1434350a476005c85dc39173ead11090
parent6bb2790f8a0cf93d3dbed6b6e986441110e6174b (diff)
V4L/DVB (6254): Add videobuf-vmalloc
Adds a newer videobuf-vmalloc module. This module uses the same videobuf controls, but implements memory allocation based on vmalloc methods. With this method, an USB driver can use video-buf, without needing to request memory from the DMA-safe area. Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
-rw-r--r--drivers/media/Kconfig4
-rw-r--r--drivers/media/video/Kconfig2
-rw-r--r--drivers/media/video/Makefile1
-rw-r--r--drivers/media/video/videobuf-vmalloc.c384
-rw-r--r--include/media/videobuf-vmalloc.h44
5 files changed, 434 insertions, 1 deletions
diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index 28ee65c53917..fc3ea4c67ccb 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -134,6 +134,10 @@ config VIDEOBUF_DMA_SG
134 select VIDEOBUF_GEN 134 select VIDEOBUF_GEN
135 tristate 135 tristate
136 136
137config VIDEOBUF_VMALLOC
138 select VIDEOBUF_GEN
139 tristate
140
137config VIDEO_BUF_DVB 141config VIDEO_BUF_DVB
138 tristate 142 tristate
139 143
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index c4f424422f70..2e571eb9313a 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -364,7 +364,7 @@ endmenu # encoder / decoder chips
364config VIDEO_VIVI 364config VIDEO_VIVI
365 tristate "Virtual Video Driver" 365 tristate "Virtual Video Driver"
366 depends on VIDEO_V4L2 && !SPARC32 && !SPARC64 && PCI 366 depends on VIDEO_V4L2 && !SPARC32 && !SPARC64 && PCI
367 select VIDEOBUF_DMA_SG 367 select VIDEOBUF_VMALLOC
368 default n 368 default n
369 ---help--- 369 ---help---
370 Enables a virtual video driver. This device shows a color bar 370 Enables a virtual video driver. This device shows a color bar
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index cb300241eb89..c8140aec1a16 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -89,6 +89,7 @@ obj-$(CONFIG_TUNER_TEA5761) += tea5761.o
89 89
90obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o 90obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o
91obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o 91obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o
92obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o
92obj-$(CONFIG_VIDEO_BUF_DVB) += video-buf-dvb.o 93obj-$(CONFIG_VIDEO_BUF_DVB) += video-buf-dvb.o
93obj-$(CONFIG_VIDEO_BTCX) += btcx-risc.o 94obj-$(CONFIG_VIDEO_BTCX) += btcx-risc.o
94obj-$(CONFIG_VIDEO_TVEEPROM) += tveeprom.o 95obj-$(CONFIG_VIDEO_TVEEPROM) += tveeprom.o
diff --git a/drivers/media/video/videobuf-vmalloc.c b/drivers/media/video/videobuf-vmalloc.c
new file mode 100644
index 000000000000..993d5285e18e
--- /dev/null
+++ b/drivers/media/video/videobuf-vmalloc.c
@@ -0,0 +1,384 @@
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;
65 struct videbuf_vmalloc_memory *mem;
66 int i;
67
68 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
69 map->count,vma->vm_start,vma->vm_end);
70
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;
79
80 if (!mem)
81 continue;
82
83 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
84
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]);
90 }
91 mutex_unlock(&q->lock);
92 kfree(map);
93 }
94 return;
95}
96
97static struct vm_operations_struct videobuf_vm_ops =
98{
99 .open = videobuf_vm_open,
100 .close = videobuf_vm_close,
101};
102
103/* ---------------------------------------------------------------------
104 * vmalloc handlers for the generic methods
105 */
106
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
111 */
112
113static void *__videobuf_alloc(size_t size)
114{
115 struct videbuf_vmalloc_memory *mem;
116 struct videobuf_buffer *vb;
117
118 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
119
120 mem = vb->priv = ((char *)vb)+size;
121 mem->magic=MAGIC_VMAL_MEM;
122
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));
126
127 return vb;
128}
129
130static int __videobuf_iolock (struct videobuf_queue* q,
131 struct videobuf_buffer *vb,
132 struct v4l2_framebuffer *fbuf)
133{
134 int pages;
135
136 struct videbuf_vmalloc_memory *mem=vb->priv;
137
138
139 BUG_ON(!mem);
140
141 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
142
143
144 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
145
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");
150 return -EINVAL;
151 }
152
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);
157 return -ENOMEM;
158 }
159
160 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
161 (unsigned long)mem->vmalloc,
162 pages << PAGE_SHIFT);
163
164 /* It seems that some kernel versions need to do remap *after*
165 the mmap() call
166 */
167 if (mem->vma) {
168 int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
169 kfree(mem->vma);
170 mem->vma=NULL;
171 if (retval<0) {
172 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
173 mem->vmalloc,retval);
174 return retval;
175 }
176 }
177
178 return 0;
179}
180
181static int __videobuf_sync(struct videobuf_queue *q,
182 struct videobuf_buffer *buf)
183{
184 return 0;
185}
186
187static int __videobuf_mmap_free(struct videobuf_queue *q)
188{
189 unsigned int i;
190
191 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
192 if (q->bufs[i]) {
193 struct videbuf_vmalloc_memory *mem=q->bufs[i]->priv;
194 if (mem && mem->map)
195 return -EBUSY;
196 }
197 }
198
199 return 0;
200}
201
202static int __videobuf_mmap_mapper(struct videobuf_queue *q,
203 struct vm_area_struct *vma)
204{
205 struct videbuf_vmalloc_memory *mem;
206 struct videobuf_mapping *map;
207 unsigned int first;
208 int retval;
209 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
210
211 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
212 return -EINVAL;
213
214 /* look for first buffer to map */
215 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
216 if (NULL == q->bufs[first])
217 continue;
218
219 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
220 continue;
221 if (q->bufs[first]->boff == offset)
222 break;
223 }
224 if (VIDEO_MAX_FRAME == first) {
225 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
226 (vma->vm_pgoff << PAGE_SHIFT));
227 return -EINVAL;
228 }
229 mem=q->bufs[first]->priv;
230 BUG_ON (!mem);
231 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
232
233 /* create mapping + update buffer list */
234 map = mem->map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
235 if (NULL == map)
236 return -ENOMEM;
237
238 map->start = vma->vm_start;
239 map->end = vma->vm_end;
240 map->q = q;
241
242 q->bufs[first]->baddr = vma->vm_start;
243
244 vma->vm_ops = &videobuf_vm_ops;
245 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
246 vma->vm_private_data = map;
247
248 /* Try to remap memory */
249 retval=remap_vmalloc_range(vma, mem->vmalloc,0);
250 if (retval<0) {
251 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
252 mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
253 if (!mem->vma) {
254 kfree(map);
255 mem->map=NULL;
256 return -ENOMEM;
257 }
258 memcpy(mem->vma,vma,sizeof(*vma));
259 }
260
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);
265
266 videobuf_vm_open(vma);
267
268 return (0);
269}
270
271static int __videobuf_is_mmapped (struct videobuf_buffer *buf)
272{
273 struct videbuf_vmalloc_memory *mem=buf->priv;
274 BUG_ON (!mem);
275 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
276
277 return (mem->map)?1:0;
278}
279
280static int __videobuf_copy_to_user ( struct videobuf_queue *q,
281 char __user *data, size_t count,
282 int nonblocking )
283{
284 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
285 BUG_ON (!mem);
286 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
287
288 BUG_ON (!mem->vmalloc);
289
290 /* copy to userspace */
291 if (count > q->read_buf->size - q->read_off)
292 count = q->read_buf->size - q->read_off;
293
294 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
295 return -EFAULT;
296
297 return count;
298}
299
300static int __videobuf_copy_stream ( struct videobuf_queue *q,
301 char __user *data, size_t count, size_t pos,
302 int vbihack, int nonblocking )
303{
304 unsigned int *fc;
305 struct videbuf_vmalloc_memory *mem=q->read_buf->priv;
306 BUG_ON (!mem);
307 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
308
309 if (vbihack) {
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);
318 }
319
320 /* copy stuff using the common method */
321 count = __videobuf_copy_to_user (q,data,count,nonblocking);
322
323 if ( (count==-EFAULT) && (0 == pos) )
324 return -EFAULT;
325
326 return count;
327}
328
329static struct videobuf_qtype_ops qops = {
330 .magic = MAGIC_QTYPE_OPS,
331
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,
340};
341
342void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
343 struct videobuf_queue_ops *ops,
344 void *dev,
345 spinlock_t *irqlock,
346 enum v4l2_buf_type type,
347 enum v4l2_field field,
348 unsigned int msize,
349 void *priv)
350{
351 videobuf_queue_init(q, ops, dev, irqlock, type, field, msize, priv);
352 q->int_ops=&qops;
353}
354
355EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
356
357void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
358{
359 struct videbuf_vmalloc_memory *mem=buf->priv;
360 BUG_ON (!mem);
361 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
362
363 return mem->vmalloc;
364}
365EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
366
367void videobuf_vmalloc_free (struct videobuf_buffer *buf)
368{
369 struct videbuf_vmalloc_memory *mem=buf->priv;
370 BUG_ON (!mem);
371
372 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
373
374 vfree(mem->vmalloc);
375
376 return;
377}
378EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
379
380/*
381 * Local variables:
382 * c-basic-offset: 8
383 * End:
384 */
diff --git a/include/media/videobuf-vmalloc.h b/include/media/videobuf-vmalloc.h
new file mode 100644
index 000000000000..5fff68df6689
--- /dev/null
+++ b/include/media/videobuf-vmalloc.h
@@ -0,0 +1,44 @@
1/*
2 * helper functions for vmalloc 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 <media/videobuf-core.h>
17
18/* --------------------------------------------------------------------- */
19
20struct videbuf_vmalloc_memory
21{
22 u32 magic;
23
24 /* for mmap'ed buffers */
25 struct videobuf_mapping *map;
26
27 void *vmalloc;
28
29 /* remap_vmalloc_range seems to need to run after mmap() on some cases */
30 struct vm_area_struct *vma;
31};
32
33void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
34 struct videobuf_queue_ops *ops,
35 void *dev,
36 spinlock_t *irqlock,
37 enum v4l2_buf_type type,
38 enum v4l2_field field,
39 unsigned int msize,
40 void *priv);
41
42void *videobuf_to_vmalloc (struct videobuf_buffer *buf);
43
44void videobuf_vmalloc_free (struct videobuf_buffer *buf);