blob: 4a2508dfa0e3cd02f4419bc52b5132ad4fbd6b99 [file] [log] [blame]
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -03001/*
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
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030036static int debug;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030037module_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) \
Brandon Philips493977f2007-11-30 22:37:28 -030044 printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030045
46
47/***************************************************************************/
48
49static void
50videobuf_vm_open(struct vm_area_struct *vma)
51{
52 struct videobuf_mapping *map = vma->vm_private_data;
53
Brandon Philips0b296692007-12-08 23:05:53 -030054 dprintk(2,"vm_open %p [count=%u,vma=%08lx-%08lx]\n",map,
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030055 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;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030065 int i;
66
Brandon Philips0b296692007-12-08 23:05:53 -030067 dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map,
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030068 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);
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -030073 mutex_lock(&q->vb_lock);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030074 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
75 if (NULL == q->bufs[i])
76 continue;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030077
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -030078 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030079 continue;
Mauro Carvalho Chehab123f8ef2007-09-06 20:11:35 -030080
81 q->ops->buf_release(q,q->bufs[i]);
82
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -030083 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030084 q->bufs[i]->baddr = 0;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030085 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -030086 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030087 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, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300105 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300106 */
107
108static void *__videobuf_alloc(size_t size)
109{
Brandon Philips384b8352008-02-04 20:52:21 -0300110 struct videobuf_vmalloc_memory *mem;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300111 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;
Brandon Philips384b8352008-02-04 20:52:21 -0300130 struct videobuf_vmalloc_memory *mem=vb->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300131
132 BUG_ON(!mem);
133
134 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
135
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300136 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
137
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");
142 return -EINVAL;
143 }
144
145 /* FIXME: should be tested with kernel mmap mem */
146 mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
147 if (NULL == mem->vmalloc) {
Mauro Carvalho Chehabe78dcf52007-09-06 19:08:24 -0300148 printk(KERN_ERR "vmalloc (%d pages) failed\n",pages);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300149 return -ENOMEM;
150 }
151
152 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
153 (unsigned long)mem->vmalloc,
154 pages << PAGE_SHIFT);
155
156 /* It seems that some kernel versions need to do remap *after*
157 the mmap() call
158 */
159 if (mem->vma) {
160 int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
161 kfree(mem->vma);
162 mem->vma=NULL;
163 if (retval<0) {
164 dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
165 mem->vmalloc,retval);
166 return retval;
167 }
168 }
169
170 return 0;
171}
172
173static int __videobuf_sync(struct videobuf_queue *q,
174 struct videobuf_buffer *buf)
175{
176 return 0;
177}
178
179static int __videobuf_mmap_free(struct videobuf_queue *q)
180{
181 unsigned int i;
182
183 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
184 if (q->bufs[i]) {
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300185 if (q->bufs[i]->map)
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300186 return -EBUSY;
187 }
188 }
189
190 return 0;
191}
192
193static int __videobuf_mmap_mapper(struct videobuf_queue *q,
194 struct vm_area_struct *vma)
195{
Brandon Philips384b8352008-02-04 20:52:21 -0300196 struct videobuf_vmalloc_memory *mem;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300197 struct videobuf_mapping *map;
198 unsigned int first;
199 int retval;
200 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
201
202 if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
203 return -EINVAL;
204
205 /* look for first buffer to map */
206 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
207 if (NULL == q->bufs[first])
208 continue;
209
210 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
211 continue;
212 if (q->bufs[first]->boff == offset)
213 break;
214 }
215 if (VIDEO_MAX_FRAME == first) {
216 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
217 (vma->vm_pgoff << PAGE_SHIFT));
218 return -EINVAL;
219 }
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300220
221 /* create mapping + update buffer list */
Brandon Philips0b296692007-12-08 23:05:53 -0300222 map = q->bufs[first]->map = kzalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300223 if (NULL == map)
224 return -ENOMEM;
225
226 map->start = vma->vm_start;
227 map->end = vma->vm_end;
228 map->q = q;
229
230 q->bufs[first]->baddr = vma->vm_start;
231
232 vma->vm_ops = &videobuf_vm_ops;
233 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
234 vma->vm_private_data = map;
235
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300236 mem=q->bufs[first]->priv;
237 BUG_ON (!mem);
238 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
239
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300240 /* Try to remap memory */
241 retval=remap_vmalloc_range(vma, mem->vmalloc,0);
242 if (retval<0) {
243 dprintk(1,"mmap: postponing remap_vmalloc_range\n");
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300244
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300245 mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
246 if (!mem->vma) {
247 kfree(map);
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300248 q->bufs[first]->map=NULL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300249 return -ENOMEM;
250 }
251 memcpy(mem->vma,vma,sizeof(*vma));
252 }
253
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);
258
259 videobuf_vm_open(vma);
260
261 return (0);
262}
263
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300264static int __videobuf_copy_to_user ( struct videobuf_queue *q,
265 char __user *data, size_t count,
266 int nonblocking )
267{
Brandon Philips384b8352008-02-04 20:52:21 -0300268 struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300269 BUG_ON (!mem);
270 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
271
272 BUG_ON (!mem->vmalloc);
273
274 /* copy to userspace */
275 if (count > q->read_buf->size - q->read_off)
276 count = q->read_buf->size - q->read_off;
277
278 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
279 return -EFAULT;
280
281 return count;
282}
283
284static int __videobuf_copy_stream ( struct videobuf_queue *q,
285 char __user *data, size_t count, size_t pos,
286 int vbihack, int nonblocking )
287{
288 unsigned int *fc;
Brandon Philips384b8352008-02-04 20:52:21 -0300289 struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300290 BUG_ON (!mem);
291 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
292
293 if (vbihack) {
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);
302 }
303
304 /* copy stuff using the common method */
305 count = __videobuf_copy_to_user (q,data,count,nonblocking);
306
307 if ( (count==-EFAULT) && (0 == pos) )
308 return -EFAULT;
309
310 return count;
311}
312
313static struct videobuf_qtype_ops qops = {
314 .magic = MAGIC_QTYPE_OPS,
315
316 .alloc = __videobuf_alloc,
317 .iolock = __videobuf_iolock,
318 .sync = __videobuf_sync,
319 .mmap_free = __videobuf_mmap_free,
320 .mmap_mapper = __videobuf_mmap_mapper,
Al Viro13bcd5d2007-10-13 08:25:24 +0100321 .video_copy_to_user = __videobuf_copy_to_user,
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300322 .copy_stream = __videobuf_copy_stream,
323};
324
325void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
326 struct videobuf_queue_ops *ops,
327 void *dev,
328 spinlock_t *irqlock,
329 enum v4l2_buf_type type,
330 enum v4l2_field field,
331 unsigned int msize,
332 void *priv)
333{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300334 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
335 priv, &qops);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300336}
337
338EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
339
340void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
341{
Brandon Philips384b8352008-02-04 20:52:21 -0300342 struct videobuf_vmalloc_memory *mem=buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300343 BUG_ON (!mem);
344 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
345
346 return mem->vmalloc;
347}
348EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
349
350void videobuf_vmalloc_free (struct videobuf_buffer *buf)
351{
Brandon Philips384b8352008-02-04 20:52:21 -0300352 struct videobuf_vmalloc_memory *mem=buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300353 BUG_ON (!mem);
354
355 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
356
357 vfree(mem->vmalloc);
Mauro Carvalho Chehabc520a492007-09-06 18:55:07 -0300358 mem->vmalloc=NULL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300359
360 return;
361}
362EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
363
364/*
365 * Local variables:
366 * c-basic-offset: 8
367 * End:
368 */