blob: 44ee408e145ff51e950c8a152cf44aa073359a1b [file] [log] [blame]
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001/*
2 * helper functions for PCI DMA 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 * Highly based on video-buf written originally by:
12 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
13 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
14 * (c) 2006 Ted Walther and John Sokol
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2
19 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26
27#include <linux/pci.h>
28#include <linux/vmalloc.h>
29#include <linux/pagemap.h>
Ralf Baechle11763602007-10-23 20:42:11 +020030#include <linux/scatterlist.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030031#include <asm/page.h>
32#include <asm/pgtable.h>
33
34#include <media/videobuf-dma-sg.h>
35
36#define MAGIC_DMABUF 0x19721112
37#define MAGIC_SG_MEM 0x17890714
38
39#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
40 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
41
42static int debug = 0;
43module_param(debug, int, 0644);
44
45MODULE_DESCRIPTION("helper module to manage video4linux pci dma sg buffers");
46MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
47MODULE_LICENSE("GPL");
48
49#define dprintk(level, fmt, arg...) if (debug >= level) \
50 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
51
52/* --------------------------------------------------------------------- */
53
54struct scatterlist*
55videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
56{
57 struct scatterlist *sglist;
58 struct page *pg;
59 int i;
60
61 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
62 if (NULL == sglist)
63 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020064 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030065 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
66 pg = vmalloc_to_page(virt);
67 if (NULL == pg)
68 goto err;
69 BUG_ON(PageHighMem(pg));
Jens Axboe642f1492007-10-24 11:20:47 +020070 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030071 }
72 return sglist;
73
74 err:
75 kfree(sglist);
76 return NULL;
77}
78
79struct scatterlist*
80videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
81{
82 struct scatterlist *sglist;
83 int i = 0;
84
85 if (NULL == pages[0])
86 return NULL;
87 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
88 if (NULL == sglist)
89 return NULL;
Jens Axboe45711f12007-10-22 21:19:53 +020090 sg_init_table(sglist, nr_pages);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030091
92 if (NULL == pages[0])
93 goto nopage;
94 if (PageHighMem(pages[0]))
95 /* DMA to highmem pages might not work */
96 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +020097 sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030098 for (i = 1; i < nr_pages; i++) {
99 if (NULL == pages[i])
100 goto nopage;
101 if (PageHighMem(pages[i]))
102 goto highmem;
Jens Axboe642f1492007-10-24 11:20:47 +0200103 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300104 }
105 return sglist;
106
107 nopage:
108 dprintk(2,"sgl: oops - no page\n");
109 kfree(sglist);
110 return NULL;
111
112 highmem:
113 dprintk(2,"sgl: oops - highmem page\n");
114 kfree(sglist);
115 return NULL;
116}
117
118/* --------------------------------------------------------------------- */
119
120struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
121{
122 struct videbuf_pci_sg_memory *mem=buf->priv;
123 BUG_ON (!mem);
124
125 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
126
127 return &mem->dma;
128}
129
130void videobuf_dma_init(struct videobuf_dmabuf *dma)
131{
132 memset(dma,0,sizeof(*dma));
133 dma->magic = MAGIC_DMABUF;
134}
135
Maxim Levitsky99001322007-09-27 20:34:09 -0300136static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
137 int direction, unsigned long data, unsigned long size)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138{
139 unsigned long first,last;
140 int err, rw = 0;
141
142 dma->direction = direction;
143 switch (dma->direction) {
144 case PCI_DMA_FROMDEVICE: rw = READ; break;
145 case PCI_DMA_TODEVICE: rw = WRITE; break;
146 default: BUG();
147 }
148
149 first = (data & PAGE_MASK) >> PAGE_SHIFT;
150 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
151 dma->offset = data & ~PAGE_MASK;
152 dma->nr_pages = last-first+1;
153 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
154 GFP_KERNEL);
155 if (NULL == dma->pages)
156 return -ENOMEM;
157 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
158 data,size,dma->nr_pages);
159
160 dma->varea = (void *) data;
161
Maxim Levitsky99001322007-09-27 20:34:09 -0300162
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300163 err = get_user_pages(current,current->mm,
164 data & PAGE_MASK, dma->nr_pages,
165 rw == READ, 1, /* force */
166 dma->pages, NULL);
Maxim Levitsky99001322007-09-27 20:34:09 -0300167
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300168 if (err != dma->nr_pages) {
169 dma->nr_pages = (err >= 0) ? err : 0;
170 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
171 return err < 0 ? err : -EINVAL;
172 }
173 return 0;
174}
175
Maxim Levitsky99001322007-09-27 20:34:09 -0300176int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
177 unsigned long data, unsigned long size)
178{
179 int ret;
180 down_read(&current->mm->mmap_sem);
181 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
182 up_read(&current->mm->mmap_sem);
183
184 return ret;
185}
186
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300187int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
188 int nr_pages)
189{
190 dprintk(1,"init kernel [%d pages]\n",nr_pages);
191 dma->direction = direction;
192 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
193 if (NULL == dma->vmalloc) {
194 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
195 return -ENOMEM;
196 }
197 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
198 (unsigned long)dma->vmalloc,
199 nr_pages << PAGE_SHIFT);
200 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
201 dma->nr_pages = nr_pages;
202 return 0;
203}
204
205int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
206 dma_addr_t addr, int nr_pages)
207{
208 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
209 nr_pages,(unsigned long)addr);
210 dma->direction = direction;
211 if (0 == addr)
212 return -EINVAL;
213
214 dma->bus_addr = addr;
215 dma->nr_pages = nr_pages;
216 return 0;
217}
218
219int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
220{
221 void *dev=q->dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300222
223 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
224 BUG_ON(0 == dma->nr_pages);
225
226 if (dma->pages) {
227 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
228 dma->offset);
229 }
230 if (dma->vmalloc) {
231 dma->sglist = videobuf_vmalloc_to_sg
232 (dma->vmalloc,dma->nr_pages);
233 }
234 if (dma->bus_addr) {
235 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
236 if (NULL != dma->sglist) {
237 dma->sglen = 1;
238 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
239 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
240 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
241 }
242 }
243 if (NULL == dma->sglist) {
244 dprintk(1,"scatterlist is NULL\n");
245 return -ENOMEM;
246 }
247 if (!dma->bus_addr) {
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300248 dma->sglen = pci_map_sg(dev,dma->sglist,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300249 dma->nr_pages, dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300250 if (0 == dma->sglen) {
251 printk(KERN_WARNING
252 "%s: videobuf_map_sg failed\n",__FUNCTION__);
253 kfree(dma->sglist);
254 dma->sglist = NULL;
255 dma->sglen = 0;
256 return -EIO;
257 }
258 }
259 return 0;
260}
261
262int videobuf_dma_sync(struct videobuf_queue *q,struct videobuf_dmabuf *dma)
263{
264 void *dev=q->dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300265
266 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
267 BUG_ON(!dma->sglen);
268
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300269 pci_dma_sync_sg_for_cpu (dev,dma->sglist,dma->nr_pages,dma->direction);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300270 return 0;
271}
272
273int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
274{
275 void *dev=q->dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300276
277 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
278 if (!dma->sglen)
279 return 0;
280
Mauro Carvalho Chehab5ddff432007-10-08 11:43:49 -0300281 pci_unmap_sg (dev,dma->sglist,dma->nr_pages,dma->direction);
282
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300283 kfree(dma->sglist);
284 dma->sglist = NULL;
285 dma->sglen = 0;
286 return 0;
287}
288
289int videobuf_dma_free(struct videobuf_dmabuf *dma)
290{
291 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
292 BUG_ON(dma->sglen);
293
294 if (dma->pages) {
295 int i;
296 for (i=0; i < dma->nr_pages; i++)
297 page_cache_release(dma->pages[i]);
298 kfree(dma->pages);
299 dma->pages = NULL;
300 }
301
302 vfree(dma->vmalloc);
303 dma->vmalloc = NULL;
304 dma->varea = NULL;
305
306 if (dma->bus_addr) {
307 dma->bus_addr = 0;
308 }
309 dma->direction = PCI_DMA_NONE;
310 return 0;
311}
312
313/* --------------------------------------------------------------------- */
314
315int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
316{
317 struct videobuf_queue q;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300318
319 q.dev=pci;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300320
321 return (videobuf_dma_map(&q,dma));
322}
323
324int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
325{
326 struct videobuf_queue q;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300327
328 q.dev=pci;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300329
330 return (videobuf_dma_unmap(&q,dma));
331}
332
333/* --------------------------------------------------------------------- */
334
335static void
336videobuf_vm_open(struct vm_area_struct *vma)
337{
338 struct videobuf_mapping *map = vma->vm_private_data;
339
340 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
341 map->count,vma->vm_start,vma->vm_end);
342 map->count++;
343}
344
345static void
346videobuf_vm_close(struct vm_area_struct *vma)
347{
348 struct videobuf_mapping *map = vma->vm_private_data;
349 struct videobuf_queue *q = map->q;
350 struct videbuf_pci_sg_memory *mem;
351 int i;
352
353 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
354 map->count,vma->vm_start,vma->vm_end);
355
356 map->count--;
357 if (0 == map->count) {
358 dprintk(1,"munmap %p q=%p\n",map,q);
359 mutex_lock(&q->lock);
360 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
361 if (NULL == q->bufs[i])
362 continue;
363 mem=q->bufs[i]->priv;
364
365 if (!mem)
366 continue;
367
368 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
369
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300370 if (q->bufs[i]->map != map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300371 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300372 q->bufs[i]->map = NULL;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300373 q->bufs[i]->baddr = 0;
374 q->ops->buf_release(q,q->bufs[i]);
375 }
376 mutex_unlock(&q->lock);
377 kfree(map);
378 }
379 return;
380}
381
382/*
383 * Get a anonymous page for the mapping. Make sure we can DMA to that
384 * memory location with 32bit PCI devices (i.e. don't use highmem for
385 * now ...). Bounce buffers don't work very well for the data rates
386 * video capture has.
387 */
388static struct page*
389videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
390 int *type)
391{
392 struct page *page;
393
394 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
395 vaddr,vma->vm_start,vma->vm_end);
396 if (vaddr > vma->vm_end)
397 return NOPAGE_SIGBUS;
398 page = alloc_page(GFP_USER | __GFP_DMA32);
399 if (!page)
400 return NOPAGE_OOM;
401 clear_user_page(page_address(page), vaddr, page);
402 if (type)
403 *type = VM_FAULT_MINOR;
404 return page;
405}
406
407static struct vm_operations_struct videobuf_vm_ops =
408{
409 .open = videobuf_vm_open,
410 .close = videobuf_vm_close,
411 .nopage = videobuf_vm_nopage,
412};
413
414/* ---------------------------------------------------------------------
415 * PCI handlers for the generic methods
416 */
417
418/* Allocated area consists on 3 parts:
419 struct video_buffer
420 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
421 struct videobuf_pci_sg_memory
422 */
423
424static void *__videobuf_alloc(size_t size)
425{
426 struct videbuf_pci_sg_memory *mem;
427 struct videobuf_buffer *vb;
428
429 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
430
431 mem = vb->priv = ((char *)vb)+size;
432 mem->magic=MAGIC_SG_MEM;
433
434 videobuf_dma_init(&mem->dma);
435
436 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
437 __FUNCTION__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
438 mem,(long)sizeof(*mem));
439
440 return vb;
441}
442
443static int __videobuf_iolock (struct videobuf_queue* q,
444 struct videobuf_buffer *vb,
445 struct v4l2_framebuffer *fbuf)
446{
447 int err,pages;
448 dma_addr_t bus;
449 struct videbuf_pci_sg_memory *mem=vb->priv;
450 BUG_ON(!mem);
451
452 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
453
454 switch (vb->memory) {
455 case V4L2_MEMORY_MMAP:
456 case V4L2_MEMORY_USERPTR:
457 if (0 == vb->baddr) {
458 /* no userspace addr -- kernel bounce buffer */
459 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
460 err = videobuf_dma_init_kernel( &mem->dma,
461 PCI_DMA_FROMDEVICE,
462 pages );
463 if (0 != err)
464 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300465 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300466 /* dma directly to userspace */
467 err = videobuf_dma_init_user( &mem->dma,
468 PCI_DMA_FROMDEVICE,
469 vb->baddr,vb->bsize );
470 if (0 != err)
471 return err;
Maxim Levitsky99001322007-09-27 20:34:09 -0300472 } else {
473 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
474 buffers can only be called from videobuf_qbuf
475 we take current->mm->mmap_sem there, to prevent
476 locking inversion, so don't take it here */
477
478 err = videobuf_dma_init_user_locked(&mem->dma,
479 PCI_DMA_FROMDEVICE,
480 vb->baddr, vb->bsize);
481 if (0 != err)
482 return err;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300483 }
484 break;
485 case V4L2_MEMORY_OVERLAY:
486 if (NULL == fbuf)
487 return -EINVAL;
488 /* FIXME: need sanity checks for vb->boff */
489 /*
490 * Using a double cast to avoid compiler warnings when
491 * building for PAE. Compiler doesn't like direct casting
492 * of a 32 bit ptr to 64 bit integer.
493 */
494 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
495 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
496 err = videobuf_dma_init_overlay(&mem->dma,PCI_DMA_FROMDEVICE,
497 bus, pages);
498 if (0 != err)
499 return err;
500 break;
501 default:
502 BUG();
503 }
504 err = videobuf_dma_map(q,&mem->dma);
505 if (0 != err)
506 return err;
507
508 return 0;
509}
510
511static int __videobuf_sync(struct videobuf_queue *q,
512 struct videobuf_buffer *buf)
513{
514 struct videbuf_pci_sg_memory *mem=buf->priv;
515 BUG_ON (!mem);
516 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
517
518 return videobuf_dma_sync(q,&mem->dma);
519}
520
521static int __videobuf_mmap_free(struct videobuf_queue *q)
522{
523 int i;
524
525 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
526 if (q->bufs[i]) {
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300527 if (q->bufs[i]->map)
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300528 return -EBUSY;
529 }
530 }
531
532 return 0;
533}
534
535static int __videobuf_mmap_mapper(struct videobuf_queue *q,
536 struct vm_area_struct *vma)
537{
538 struct videbuf_pci_sg_memory *mem;
539 struct videobuf_mapping *map;
540 unsigned int first,last,size,i;
541 int retval;
542
543 retval = -EINVAL;
544 if (!(vma->vm_flags & VM_WRITE)) {
545 dprintk(1,"mmap app bug: PROT_WRITE please\n");
546 goto done;
547 }
548 if (!(vma->vm_flags & VM_SHARED)) {
549 dprintk(1,"mmap app bug: MAP_SHARED please\n");
550 goto done;
551 }
552
553 /* look for first buffer to map */
554 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
555 if (NULL == q->bufs[first])
556 continue;
557 mem=q->bufs[first]->priv;
558 BUG_ON (!mem);
559 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
560
561 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
562 continue;
563 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
564 break;
565 }
566 if (VIDEO_MAX_FRAME == first) {
567 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
568 (vma->vm_pgoff << PAGE_SHIFT));
569 goto done;
570 }
571
572 /* look for last buffer to map */
573 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
574 if (NULL == q->bufs[last])
575 continue;
576 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
577 continue;
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300578 if (q->bufs[last]->map) {
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300579 retval = -EBUSY;
580 goto done;
581 }
582 size += q->bufs[last]->bsize;
583 if (size == (vma->vm_end - vma->vm_start))
584 break;
585 }
586 if (VIDEO_MAX_FRAME == last) {
587 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
588 (vma->vm_end - vma->vm_start));
589 goto done;
590 }
591
592 /* create mapping + update buffer list */
593 retval = -ENOMEM;
594 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
595 if (NULL == map)
596 goto done;
597 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300598 q->bufs[i]->map = map;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300599 q->bufs[i]->baddr = vma->vm_start + size;
600 }
601 map->count = 1;
602 map->start = vma->vm_start;
603 map->end = vma->vm_end;
604 map->q = q;
605 vma->vm_ops = &videobuf_vm_ops;
606 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
607 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
608 vma->vm_private_data = map;
609 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
610 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
611 retval = 0;
612
613 done:
614 return retval;
615}
616
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300617static int __videobuf_copy_to_user ( struct videobuf_queue *q,
618 char __user *data, size_t count,
619 int nonblocking )
620{
621 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
622 BUG_ON (!mem);
623 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
624
625 /* copy to userspace */
626 if (count > q->read_buf->size - q->read_off)
627 count = q->read_buf->size - q->read_off;
628
629 if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
630 return -EFAULT;
631
632 return count;
633}
634
635static int __videobuf_copy_stream ( struct videobuf_queue *q,
636 char __user *data, size_t count, size_t pos,
637 int vbihack, int nonblocking )
638{
639 unsigned int *fc;
640 struct videbuf_pci_sg_memory *mem=q->read_buf->priv;
641 BUG_ON (!mem);
642 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
643
644 if (vbihack) {
645 /* dirty, undocumented hack -- pass the frame counter
646 * within the last four bytes of each vbi data block.
647 * We need that one to maintain backward compatibility
648 * to all vbi decoding software out there ... */
649 fc = (unsigned int*)mem->dma.vmalloc;
650 fc += (q->read_buf->size>>2) -1;
651 *fc = q->read_buf->field_count >> 1;
652 dprintk(1,"vbihack: %d\n",*fc);
653 }
654
655 /* copy stuff using the common method */
656 count = __videobuf_copy_to_user (q,data,count,nonblocking);
657
658 if ( (count==-EFAULT) && (0 == pos) )
659 return -EFAULT;
660
661 return count;
662}
663
664static struct videobuf_qtype_ops pci_ops = {
665 .magic = MAGIC_QTYPE_OPS,
666
667 .alloc = __videobuf_alloc,
668 .iolock = __videobuf_iolock,
669 .sync = __videobuf_sync,
670 .mmap_free = __videobuf_mmap_free,
671 .mmap_mapper = __videobuf_mmap_mapper,
Al Viro13bcd5d2007-10-13 08:25:24 +0100672 .video_copy_to_user = __videobuf_copy_to_user,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300673 .copy_stream = __videobuf_copy_stream,
674};
675
676void *videobuf_pci_alloc (size_t size)
677{
678 struct videobuf_queue q;
679
680 /* Required to make generic handler to call __videobuf_alloc */
681 q.int_ops=&pci_ops;
682
683 q.msize=size;
684
685 return videobuf_alloc (&q);
686}
687
688void videobuf_queue_pci_init(struct videobuf_queue* q,
689 struct videobuf_queue_ops *ops,
690 void *dev,
691 spinlock_t *irqlock,
692 enum v4l2_buf_type type,
693 enum v4l2_field field,
694 unsigned int msize,
695 void *priv)
696{
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300697 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
698 priv, &pci_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300699}
700
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300701/* --------------------------------------------------------------------- */
702
703EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
704
705EXPORT_SYMBOL_GPL(videobuf_to_dma);
706EXPORT_SYMBOL_GPL(videobuf_dma_init);
707EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
708EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
709EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
710EXPORT_SYMBOL_GPL(videobuf_dma_map);
711EXPORT_SYMBOL_GPL(videobuf_dma_sync);
712EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
713EXPORT_SYMBOL_GPL(videobuf_dma_free);
714
715EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
716EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
717EXPORT_SYMBOL_GPL(videobuf_pci_alloc);
718
719EXPORT_SYMBOL_GPL(videobuf_queue_pci_init);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300720
721/*
722 * Local variables:
723 * c-basic-offset: 8
724 * End:
725 */