Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6.git] / drivers / media / video / videobuf-dma-sg.c
1 /*
2  * helper functions for SG DMA video4linux capture buffers
3  *
4  * The functions expect the hardware being able to scatter gather
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/sched.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27
28 #include <linux/dma-mapping.h>
29 #include <linux/vmalloc.h>
30 #include <linux/pagemap.h>
31 #include <linux/scatterlist.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34
35 #include <media/videobuf-dma-sg.h>
36
37 #define MAGIC_DMABUF 0x19721112
38 #define MAGIC_SG_MEM 0x17890714
39
40 #define MAGIC_CHECK(is,should)  if (unlikely((is) != (should))) \
41         { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
42
43 static int debug;
44 module_param(debug, int, 0644);
45
46 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
47 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
48 MODULE_LICENSE("GPL");
49
50 #define dprintk(level, fmt, arg...)     if (debug >= level) \
51         printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
52
53 /* --------------------------------------------------------------------- */
54
55 struct scatterlist*
56 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
57 {
58         struct scatterlist *sglist;
59         struct page *pg;
60         int i;
61
62         sglist = vmalloc(nr_pages * sizeof(*sglist));
63         if (NULL == sglist)
64                 return NULL;
65         memset(sglist, 0, nr_pages * sizeof(*sglist));
66         sg_init_table(sglist, nr_pages);
67         for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
68                 pg = vmalloc_to_page(virt);
69                 if (NULL == pg)
70                         goto err;
71                 BUG_ON(PageHighMem(pg));
72                 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
73         }
74         return sglist;
75
76  err:
77         vfree(sglist);
78         return NULL;
79 }
80
81 struct scatterlist*
82 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
83 {
84         struct scatterlist *sglist;
85         int i;
86
87         if (NULL == pages[0])
88                 return NULL;
89         sglist = vmalloc(nr_pages * sizeof(*sglist));
90         if (NULL == sglist)
91                 return NULL;
92         sg_init_table(sglist, nr_pages);
93
94         if (PageHighMem(pages[0]))
95                 /* DMA to highmem pages might not work */
96                 goto highmem;
97         sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
98         for (i = 1; i < nr_pages; i++) {
99                 if (NULL == pages[i])
100                         goto nopage;
101                 if (PageHighMem(pages[i]))
102                         goto highmem;
103                 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
104         }
105         return sglist;
106
107  nopage:
108         dprintk(2,"sgl: oops - no page\n");
109         vfree(sglist);
110         return NULL;
111
112  highmem:
113         dprintk(2,"sgl: oops - highmem page\n");
114         vfree(sglist);
115         return NULL;
116 }
117
118 /* --------------------------------------------------------------------- */
119
120 struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
121 {
122         struct videobuf_dma_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
130 void videobuf_dma_init(struct videobuf_dmabuf *dma)
131 {
132         memset(dma,0,sizeof(*dma));
133         dma->magic = MAGIC_DMABUF;
134 }
135
136 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
137                         int direction, unsigned long data, unsigned long size)
138 {
139         unsigned long first,last;
140         int err, rw = 0;
141
142         dma->direction = direction;
143         switch (dma->direction) {
144         case DMA_FROM_DEVICE:
145                 rw = READ;
146                 break;
147         case DMA_TO_DEVICE:
148                 rw = WRITE;
149                 break;
150         default:
151                 BUG();
152         }
153
154         first = (data          & PAGE_MASK) >> PAGE_SHIFT;
155         last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
156         dma->offset   = data & ~PAGE_MASK;
157         dma->nr_pages = last-first+1;
158         dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
159                              GFP_KERNEL);
160         if (NULL == dma->pages)
161                 return -ENOMEM;
162         dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
163                 data,size,dma->nr_pages);
164
165         err = get_user_pages(current,current->mm,
166                              data & PAGE_MASK, dma->nr_pages,
167                              rw == READ, 1, /* force */
168                              dma->pages, NULL);
169
170         if (err != dma->nr_pages) {
171                 dma->nr_pages = (err >= 0) ? err : 0;
172                 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
173                 return err < 0 ? err : -EINVAL;
174         }
175         return 0;
176 }
177
178 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
179                            unsigned long data, unsigned long size)
180 {
181         int ret;
182         down_read(&current->mm->mmap_sem);
183         ret = videobuf_dma_init_user_locked(dma, direction, data, size);
184         up_read(&current->mm->mmap_sem);
185
186         return ret;
187 }
188
189 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
190                              int nr_pages)
191 {
192         dprintk(1,"init kernel [%d pages]\n",nr_pages);
193         dma->direction = direction;
194         dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
195         if (NULL == dma->vmalloc) {
196                 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
197                 return -ENOMEM;
198         }
199         dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
200                                 (unsigned long)dma->vmalloc,
201                                 nr_pages << PAGE_SHIFT);
202         memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
203         dma->nr_pages = nr_pages;
204         return 0;
205 }
206
207 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
208                               dma_addr_t addr, int nr_pages)
209 {
210         dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
211                 nr_pages,(unsigned long)addr);
212         dma->direction = direction;
213         if (0 == addr)
214                 return -EINVAL;
215
216         dma->bus_addr = addr;
217         dma->nr_pages = nr_pages;
218         return 0;
219 }
220
221 int videobuf_dma_map(struct videobuf_queue* q, struct videobuf_dmabuf *dma)
222 {
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 = vmalloc(sizeof(*dma->sglist));
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) {
248                 dma->sglen = dma_map_sg(q->dev, dma->sglist,
249                                         dma->nr_pages, dma->direction);
250                 if (0 == dma->sglen) {
251                         printk(KERN_WARNING
252                                "%s: videobuf_map_sg failed\n",__func__);
253                         vfree(dma->sglist);
254                         dma->sglist = NULL;
255                         dma->sglen = 0;
256                         return -ENOMEM;
257                 }
258         }
259         return 0;
260 }
261
262 int videobuf_dma_sync(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
263 {
264         MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
265         BUG_ON(!dma->sglen);
266
267         dma_sync_sg_for_cpu(q->dev, dma->sglist, dma->nr_pages, dma->direction);
268         return 0;
269 }
270
271 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
272 {
273         MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
274         if (!dma->sglen)
275                 return 0;
276
277         dma_unmap_sg(q->dev, dma->sglist, dma->nr_pages, dma->direction);
278
279         vfree(dma->sglist);
280         dma->sglist = NULL;
281         dma->sglen = 0;
282         return 0;
283 }
284
285 int videobuf_dma_free(struct videobuf_dmabuf *dma)
286 {
287         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
288         BUG_ON(dma->sglen);
289
290         if (dma->pages) {
291                 int i;
292                 for (i=0; i < dma->nr_pages; i++)
293                         page_cache_release(dma->pages[i]);
294                 kfree(dma->pages);
295                 dma->pages = NULL;
296         }
297
298         vfree(dma->vmalloc);
299         dma->vmalloc = NULL;
300
301         if (dma->bus_addr) {
302                 dma->bus_addr = 0;
303         }
304         dma->direction = DMA_NONE;
305         return 0;
306 }
307
308 /* --------------------------------------------------------------------- */
309
310 int videobuf_sg_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
311 {
312         struct videobuf_queue q;
313
314         q.dev = dev;
315
316         return videobuf_dma_map(&q, dma);
317 }
318
319 int videobuf_sg_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
320 {
321         struct videobuf_queue q;
322
323         q.dev = dev;
324
325         return videobuf_dma_unmap(&q, dma);
326 }
327
328 /* --------------------------------------------------------------------- */
329
330 static void
331 videobuf_vm_open(struct vm_area_struct *vma)
332 {
333         struct videobuf_mapping *map = vma->vm_private_data;
334
335         dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
336                 map->count,vma->vm_start,vma->vm_end);
337         map->count++;
338 }
339
340 static void
341 videobuf_vm_close(struct vm_area_struct *vma)
342 {
343         struct videobuf_mapping *map = vma->vm_private_data;
344         struct videobuf_queue *q = map->q;
345         struct videobuf_dma_sg_memory *mem;
346         int i;
347
348         dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
349                 map->count,vma->vm_start,vma->vm_end);
350
351         map->count--;
352         if (0 == map->count) {
353                 dprintk(1,"munmap %p q=%p\n",map,q);
354                 mutex_lock(&q->vb_lock);
355                 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
356                         if (NULL == q->bufs[i])
357                                 continue;
358                         mem=q->bufs[i]->priv;
359
360                         if (!mem)
361                                 continue;
362
363                         MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
364
365                         if (q->bufs[i]->map != map)
366                                 continue;
367                         q->bufs[i]->map   = NULL;
368                         q->bufs[i]->baddr = 0;
369                         q->ops->buf_release(q,q->bufs[i]);
370                 }
371                 mutex_unlock(&q->vb_lock);
372                 kfree(map);
373         }
374         return;
375 }
376
377 /*
378  * Get a anonymous page for the mapping.  Make sure we can DMA to that
379  * memory location with 32bit PCI devices (i.e. don't use highmem for
380  * now ...).  Bounce buffers don't work very well for the data rates
381  * video capture has.
382  */
383 static int
384 videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
385 {
386         struct page *page;
387
388         dprintk(3,"fault: fault @ %08lx [vma %08lx-%08lx]\n",
389                 (unsigned long)vmf->virtual_address,vma->vm_start,vma->vm_end);
390         page = alloc_page(GFP_USER | __GFP_DMA32);
391         if (!page)
392                 return VM_FAULT_OOM;
393         clear_user_highpage(page, (unsigned long)vmf->virtual_address);
394         vmf->page = page;
395         return 0;
396 }
397
398 static const struct vm_operations_struct videobuf_vm_ops =
399 {
400         .open     = videobuf_vm_open,
401         .close    = videobuf_vm_close,
402         .fault    = videobuf_vm_fault,
403 };
404
405 /* ---------------------------------------------------------------------
406  * SG handlers for the generic methods
407  */
408
409 /* Allocated area consists on 3 parts:
410         struct video_buffer
411         struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
412         struct videobuf_dma_sg_memory
413  */
414
415 static void *__videobuf_alloc(size_t size)
416 {
417         struct videobuf_dma_sg_memory *mem;
418         struct videobuf_buffer *vb;
419
420         vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
421         if (!vb)
422                 return vb;
423
424         mem = vb->priv = ((char *)vb)+size;
425         mem->magic=MAGIC_SG_MEM;
426
427         videobuf_dma_init(&mem->dma);
428
429         dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
430                 __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
431                 mem,(long)sizeof(*mem));
432
433         return vb;
434 }
435
436 static void *__videobuf_to_vmalloc (struct videobuf_buffer *buf)
437 {
438         struct videobuf_dma_sg_memory *mem = buf->priv;
439         BUG_ON(!mem);
440
441         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
442
443         return mem->dma.vmalloc;
444 }
445
446 static int __videobuf_iolock (struct videobuf_queue* q,
447                               struct videobuf_buffer *vb,
448                               struct v4l2_framebuffer *fbuf)
449 {
450         int err,pages;
451         dma_addr_t bus;
452         struct videobuf_dma_sg_memory *mem = vb->priv;
453         BUG_ON(!mem);
454
455         MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
456
457         switch (vb->memory) {
458         case V4L2_MEMORY_MMAP:
459         case V4L2_MEMORY_USERPTR:
460                 if (0 == vb->baddr) {
461                         /* no userspace addr -- kernel bounce buffer */
462                         pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
463                         err = videobuf_dma_init_kernel( &mem->dma,
464                                                         DMA_FROM_DEVICE,
465                                                         pages );
466                         if (0 != err)
467                                 return err;
468                 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
469                         /* dma directly to userspace */
470                         err = videobuf_dma_init_user( &mem->dma,
471                                                       DMA_FROM_DEVICE,
472                                                       vb->baddr,vb->bsize );
473                         if (0 != err)
474                                 return err;
475                 } else {
476                         /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
477                         buffers can only be called from videobuf_qbuf
478                         we take current->mm->mmap_sem there, to prevent
479                         locking inversion, so don't take it here */
480
481                         err = videobuf_dma_init_user_locked(&mem->dma,
482                                                       DMA_FROM_DEVICE,
483                                                       vb->baddr, vb->bsize);
484                         if (0 != err)
485                                 return err;
486                 }
487                 break;
488         case V4L2_MEMORY_OVERLAY:
489                 if (NULL == fbuf)
490                         return -EINVAL;
491                 /* FIXME: need sanity checks for vb->boff */
492                 /*
493                  * Using a double cast to avoid compiler warnings when
494                  * building for PAE. Compiler doesn't like direct casting
495                  * of a 32 bit ptr to 64 bit integer.
496                  */
497                 bus   = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
498                 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
499                 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
500                                                 bus, pages);
501                 if (0 != err)
502                         return err;
503                 break;
504         default:
505                 BUG();
506         }
507         err = videobuf_dma_map(q, &mem->dma);
508         if (0 != err)
509                 return err;
510
511         return 0;
512 }
513
514 static int __videobuf_sync(struct videobuf_queue *q,
515                            struct videobuf_buffer *buf)
516 {
517         struct videobuf_dma_sg_memory *mem = buf->priv;
518         BUG_ON(!mem);
519         MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
520
521         return  videobuf_dma_sync(q,&mem->dma);
522 }
523
524 static int __videobuf_mmap_free(struct videobuf_queue *q)
525 {
526         int i;
527
528         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
529                 if (q->bufs[i]) {
530                         if (q->bufs[i]->map)
531                                 return -EBUSY;
532                 }
533         }
534
535         return 0;
536 }
537
538 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
539                          struct vm_area_struct *vma)
540 {
541         struct videobuf_dma_sg_memory *mem;
542         struct videobuf_mapping *map;
543         unsigned int first,last,size,i;
544         int retval;
545
546         retval = -EINVAL;
547         if (!(vma->vm_flags & VM_WRITE)) {
548                 dprintk(1,"mmap app bug: PROT_WRITE please\n");
549                 goto done;
550         }
551         if (!(vma->vm_flags & VM_SHARED)) {
552                 dprintk(1,"mmap app bug: MAP_SHARED please\n");
553                 goto done;
554         }
555
556         /* This function maintains backwards compatibility with V4L1 and will
557          * map more than one buffer if the vma length is equal to the combined
558          * size of multiple buffers than it will map them together.  See
559          * VIDIOCGMBUF in the v4l spec
560          *
561          * TODO: Allow drivers to specify if they support this mode
562          */
563
564         /* look for first buffer to map */
565         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
566                 if (NULL == q->bufs[first])
567                         continue;
568                 mem=q->bufs[first]->priv;
569                 BUG_ON(!mem);
570                 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
571
572                 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
573                         continue;
574                 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
575                         break;
576         }
577         if (VIDEO_MAX_FRAME == first) {
578                 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
579                         (vma->vm_pgoff << PAGE_SHIFT));
580                 goto done;
581         }
582
583         /* look for last buffer to map */
584         for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
585                 if (NULL == q->bufs[last])
586                         continue;
587                 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
588                         continue;
589                 if (q->bufs[last]->map) {
590                         retval = -EBUSY;
591                         goto done;
592                 }
593                 size += PAGE_ALIGN(q->bufs[last]->bsize);
594                 if (size == (vma->vm_end - vma->vm_start))
595                         break;
596         }
597         if (VIDEO_MAX_FRAME == last) {
598                 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
599                         (vma->vm_end - vma->vm_start));
600                 goto done;
601         }
602
603         /* create mapping + update buffer list */
604         retval = -ENOMEM;
605         map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
606         if (NULL == map)
607                 goto done;
608
609         size = 0;
610         for (i = first; i <= last; i++) {
611                 if (NULL == q->bufs[i])
612                         continue;
613                 q->bufs[i]->map   = map;
614                 q->bufs[i]->baddr = vma->vm_start + size;
615                 size += PAGE_ALIGN(q->bufs[i]->bsize);
616         }
617
618         map->count    = 1;
619         map->start    = vma->vm_start;
620         map->end      = vma->vm_end;
621         map->q        = q;
622         vma->vm_ops   = &videobuf_vm_ops;
623         vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
624         vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
625         vma->vm_private_data = map;
626         dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
627                 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
628         retval = 0;
629
630  done:
631         return retval;
632 }
633
634 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
635                                 char __user *data, size_t count,
636                                 int nonblocking )
637 {
638         struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
639         BUG_ON(!mem);
640         MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
641
642         /* copy to userspace */
643         if (count > q->read_buf->size - q->read_off)
644                 count = q->read_buf->size - q->read_off;
645
646         if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
647                 return -EFAULT;
648
649         return count;
650 }
651
652 static int __videobuf_copy_stream ( struct videobuf_queue *q,
653                                 char __user *data, size_t count, size_t pos,
654                                 int vbihack, int nonblocking )
655 {
656         unsigned int  *fc;
657         struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
658         BUG_ON(!mem);
659         MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
660
661         if (vbihack) {
662                 /* dirty, undocumented hack -- pass the frame counter
663                         * within the last four bytes of each vbi data block.
664                         * We need that one to maintain backward compatibility
665                         * to all vbi decoding software out there ... */
666                 fc  = (unsigned int*)mem->dma.vmalloc;
667                 fc += (q->read_buf->size>>2) -1;
668                 *fc = q->read_buf->field_count >> 1;
669                 dprintk(1,"vbihack: %d\n",*fc);
670         }
671
672         /* copy stuff using the common method */
673         count = __videobuf_copy_to_user (q,data,count,nonblocking);
674
675         if ( (count==-EFAULT) && (0 == pos) )
676                 return -EFAULT;
677
678         return count;
679 }
680
681 static struct videobuf_qtype_ops sg_ops = {
682         .magic        = MAGIC_QTYPE_OPS,
683
684         .alloc        = __videobuf_alloc,
685         .iolock       = __videobuf_iolock,
686         .sync         = __videobuf_sync,
687         .mmap_free    = __videobuf_mmap_free,
688         .mmap_mapper  = __videobuf_mmap_mapper,
689         .video_copy_to_user = __videobuf_copy_to_user,
690         .copy_stream  = __videobuf_copy_stream,
691         .vmalloc      = __videobuf_to_vmalloc,
692 };
693
694 void *videobuf_sg_alloc(size_t size)
695 {
696         struct videobuf_queue q;
697
698         /* Required to make generic handler to call __videobuf_alloc */
699         q.int_ops = &sg_ops;
700
701         q.msize = size;
702
703         return videobuf_alloc(&q);
704 }
705
706 void videobuf_queue_sg_init(struct videobuf_queue* q,
707                          const struct videobuf_queue_ops *ops,
708                          struct device *dev,
709                          spinlock_t *irqlock,
710                          enum v4l2_buf_type type,
711                          enum v4l2_field field,
712                          unsigned int msize,
713                          void *priv)
714 {
715         videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
716                                  priv, &sg_ops);
717 }
718
719 /* --------------------------------------------------------------------- */
720
721 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
722
723 EXPORT_SYMBOL_GPL(videobuf_to_dma);
724 EXPORT_SYMBOL_GPL(videobuf_dma_init);
725 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
726 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
727 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
728 EXPORT_SYMBOL_GPL(videobuf_dma_map);
729 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
730 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
731 EXPORT_SYMBOL_GPL(videobuf_dma_free);
732
733 EXPORT_SYMBOL_GPL(videobuf_sg_dma_map);
734 EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap);
735 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
736
737 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
738
739 /*
740  * Local variables:
741  * c-basic-offset: 8
742  * End:
743  */