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