blob: 075acdf6b7c71ec4b670c565c46cbc9a9aca6d10 [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
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -030081 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030082 q->bufs[i]->baddr = 0;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030083 }
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -030084 mutex_unlock(&q->vb_lock);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -030085 kfree(map);
86 }
87 return;
88}
89
90static struct vm_operations_struct videobuf_vm_ops =
91{
92 .open = videobuf_vm_open,
93 .close = videobuf_vm_close,
94};
95
96/* ---------------------------------------------------------------------
97 * vmalloc handlers for the generic methods
98 */
99
100/* Allocated area consists on 3 parts:
101 struct video_buffer
102 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300103 struct videobuf_dma_sg_memory
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300104 */
105
106static void *__videobuf_alloc(size_t size)
107{
Brandon Philips384b8352008-02-04 20:52:21 -0300108 struct videobuf_vmalloc_memory *mem;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300109 struct videobuf_buffer *vb;
110
111 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
112
113 mem = vb->priv = ((char *)vb)+size;
114 mem->magic=MAGIC_VMAL_MEM;
115
116 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300117 __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300118 mem,(long)sizeof(*mem));
119
120 return vb;
121}
122
123static int __videobuf_iolock (struct videobuf_queue* q,
124 struct videobuf_buffer *vb,
125 struct v4l2_framebuffer *fbuf)
126{
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300127 struct videobuf_vmalloc_memory *mem = vb->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300128
129 BUG_ON(!mem);
130
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300131 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300132
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300133 switch (vb->memory) {
134 case V4L2_MEMORY_MMAP:
135 dprintk(1, "%s memory method MMAP\n", __func__);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300136
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300137 /* All handling should be done by __videobuf_mmap_mapper() */
138 if (!mem->vmalloc) {
139 printk(KERN_ERR "memory is not alloced/mmapped.\n");
140 return -EINVAL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300141 }
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300142 break;
143 case V4L2_MEMORY_USERPTR:
144 {
145 int pages = PAGE_ALIGN(vb->size);
146
147 dprintk(1, "%s memory method USERPTR\n", __func__);
148
149#if 1
150 if (vb->baddr) {
151 printk(KERN_ERR "USERPTR is currently not supported\n");
152 return -EINVAL;
153 }
154#endif
155
156 /* The only USERPTR currently supported is the one needed for
157 read() method.
158 */
159
160 mem->vmalloc = vmalloc_user(pages);
161 if (!mem->vmalloc) {
162 printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
163 return -ENOMEM;
164 }
165 dprintk(1, "vmalloc is at addr %p (%d pages)\n",
166 mem->vmalloc, pages);
167
168#if 0
169 int rc;
170 /* Kernel userptr is used also by read() method. In this case,
171 there's no need to remap, since data will be copied to user
172 */
173 if (!vb->baddr)
174 return 0;
175
176 /* FIXME: to properly support USERPTR, remap should occur.
177 The code bellow won't work, since mem->vma = NULL
178 */
179 /* Try to remap memory */
180 rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0);
181 if (rc < 0) {
182 printk(KERN_ERR "mmap: remap failed with error %d. ", rc);
183 return -ENOMEM;
184 }
185#endif
186
187 break;
188 }
189 case V4L2_MEMORY_OVERLAY:
190 default:
191 dprintk(1, "%s memory method OVERLAY/unknown\n", __func__);
192
193 /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
194 printk(KERN_ERR "Memory method currently unsupported.\n");
195 return -EINVAL;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300196 }
197
198 return 0;
199}
200
201static int __videobuf_sync(struct videobuf_queue *q,
202 struct videobuf_buffer *buf)
203{
204 return 0;
205}
206
207static int __videobuf_mmap_free(struct videobuf_queue *q)
208{
209 unsigned int i;
210
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300211 dprintk(1, "%s\n", __func__);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300212 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
213 if (q->bufs[i]) {
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300214 if (q->bufs[i]->map)
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300215 return -EBUSY;
216 }
217 }
218
219 return 0;
220}
221
222static int __videobuf_mmap_mapper(struct videobuf_queue *q,
223 struct vm_area_struct *vma)
224{
Brandon Philips384b8352008-02-04 20:52:21 -0300225 struct videobuf_vmalloc_memory *mem;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300226 struct videobuf_mapping *map;
227 unsigned int first;
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300228 int retval, pages;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300229 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
230
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300231 dprintk(1, "%s\n", __func__);
232 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300233 return -EINVAL;
234
235 /* look for first buffer to map */
236 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
237 if (NULL == q->bufs[first])
238 continue;
239
240 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
241 continue;
242 if (q->bufs[first]->boff == offset)
243 break;
244 }
245 if (VIDEO_MAX_FRAME == first) {
246 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
247 (vma->vm_pgoff << PAGE_SHIFT));
248 return -EINVAL;
249 }
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300250
251 /* create mapping + update buffer list */
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300252 map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300253 if (NULL == map)
254 return -ENOMEM;
255
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300256 q->bufs[first]->map = map;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300257 map->start = vma->vm_start;
258 map->end = vma->vm_end;
259 map->q = q;
260
261 q->bufs[first]->baddr = vma->vm_start;
262
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300263 mem = q->bufs[first]->priv;
264 BUG_ON(!mem);
265 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
266
267 pages = PAGE_ALIGN(vma->vm_end - vma->vm_start);
268 mem->vmalloc = vmalloc_user(pages);
269 if (!mem->vmalloc) {
270 printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
271 goto error;
272 }
273 dprintk(1, "vmalloc is at addr %p (%d pages)\n",
274 mem->vmalloc, pages);
275
276 /* Try to remap memory */
277 retval = remap_vmalloc_range(vma, mem->vmalloc, 0);
278 if (retval < 0) {
279 printk(KERN_ERR "mmap: remap failed with error %d. ", retval);
280 vfree(mem->vmalloc);
281 goto error;
282 }
283
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300284 vma->vm_ops = &videobuf_vm_ops;
285 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
286 vma->vm_private_data = map;
287
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300288 dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300289 map, q, vma->vm_start, vma->vm_end,
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300290 (long int) q->bufs[first]->bsize,
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300291 vma->vm_pgoff, first);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300292
293 videobuf_vm_open(vma);
294
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300295 return 0;
296
297error:
298 mem = NULL;
299 kfree(map);
300 return -ENOMEM;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300301}
302
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300303static int __videobuf_copy_to_user ( struct videobuf_queue *q,
304 char __user *data, size_t count,
305 int nonblocking )
306{
Brandon Philips384b8352008-02-04 20:52:21 -0300307 struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300308 BUG_ON (!mem);
309 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
310
311 BUG_ON (!mem->vmalloc);
312
313 /* copy to userspace */
314 if (count > q->read_buf->size - q->read_off)
315 count = q->read_buf->size - q->read_off;
316
317 if (copy_to_user(data, mem->vmalloc+q->read_off, count))
318 return -EFAULT;
319
320 return count;
321}
322
323static int __videobuf_copy_stream ( struct videobuf_queue *q,
324 char __user *data, size_t count, size_t pos,
325 int vbihack, int nonblocking )
326{
327 unsigned int *fc;
Brandon Philips384b8352008-02-04 20:52:21 -0300328 struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300329 BUG_ON (!mem);
330 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
331
332 if (vbihack) {
333 /* dirty, undocumented hack -- pass the frame counter
334 * within the last four bytes of each vbi data block.
335 * We need that one to maintain backward compatibility
336 * to all vbi decoding software out there ... */
337 fc = (unsigned int*)mem->vmalloc;
338 fc += (q->read_buf->size>>2) -1;
339 *fc = q->read_buf->field_count >> 1;
340 dprintk(1,"vbihack: %d\n",*fc);
341 }
342
343 /* copy stuff using the common method */
344 count = __videobuf_copy_to_user (q,data,count,nonblocking);
345
346 if ( (count==-EFAULT) && (0 == pos) )
347 return -EFAULT;
348
349 return count;
350}
351
352static struct videobuf_qtype_ops qops = {
353 .magic = MAGIC_QTYPE_OPS,
354
355 .alloc = __videobuf_alloc,
356 .iolock = __videobuf_iolock,
357 .sync = __videobuf_sync,
358 .mmap_free = __videobuf_mmap_free,
359 .mmap_mapper = __videobuf_mmap_mapper,
Al Viro13bcd5d2007-10-13 08:25:24 +0100360 .video_copy_to_user = __videobuf_copy_to_user,
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300361 .copy_stream = __videobuf_copy_stream,
362};
363
364void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
365 struct videobuf_queue_ops *ops,
366 void *dev,
367 spinlock_t *irqlock,
368 enum v4l2_buf_type type,
369 enum v4l2_field field,
370 unsigned int msize,
371 void *priv)
372{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300373 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
374 priv, &qops);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300375}
376
377EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
378
379void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
380{
Brandon Philips384b8352008-02-04 20:52:21 -0300381 struct videobuf_vmalloc_memory *mem=buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300382 BUG_ON (!mem);
383 MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
384
385 return mem->vmalloc;
386}
387EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
388
389void videobuf_vmalloc_free (struct videobuf_buffer *buf)
390{
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300391 struct videobuf_vmalloc_memory *mem = buf->priv;
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300392
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300393 if (!mem)
394 return;
395
396 MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300397
398 vfree(mem->vmalloc);
Mauro Carvalho Chehab968ced72008-04-13 14:58:21 -0300399 mem->vmalloc = NULL;
400
401
402
403 /* FIXME: need to do buf->priv = NULL? */
Mauro Carvalho Chehab87b9ad02007-08-02 23:31:33 -0300404
405 return;
406}
407EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
408
409/*
410 * Local variables:
411 * c-basic-offset: 8
412 * End:
413 */