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@kernel.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@kernel.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/mm.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)						\
41 	if (unlikely((is) != (should))) {				\
42 		printk(KERN_ERR "magic mismatch: %x (expected %x)\n",	\
43 				is, should);				\
44 		BUG();							\
45 	}
46 
47 static int debug;
48 module_param(debug, int, 0644);
49 
50 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
51 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
52 MODULE_LICENSE("GPL");
53 
54 #define dprintk(level, fmt, arg...)					\
55 	if (debug >= level)						\
56 		printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
57 
58 /* --------------------------------------------------------------------- */
59 
60 /*
61  * Return a scatterlist for some page-aligned vmalloc()'ed memory
62  * block (NULL on errors).  Memory for the scatterlist is allocated
63  * using kmalloc.  The caller must free the memory.
64  */
videobuf_vmalloc_to_sg(unsigned char * virt,int nr_pages)65 static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
66 						  int nr_pages)
67 {
68 	struct scatterlist *sglist;
69 	struct page *pg;
70 	int i;
71 
72 	sglist = vzalloc(array_size(nr_pages, sizeof(*sglist)));
73 	if (NULL == sglist)
74 		return NULL;
75 	sg_init_table(sglist, nr_pages);
76 	for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
77 		pg = vmalloc_to_page(virt);
78 		if (NULL == pg)
79 			goto err;
80 		BUG_ON(PageHighMem(pg));
81 		sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
82 	}
83 	return sglist;
84 
85 err:
86 	vfree(sglist);
87 	return NULL;
88 }
89 
90 /*
91  * Return a scatterlist for a an array of userpages (NULL on errors).
92  * Memory for the scatterlist is allocated using kmalloc.  The caller
93  * must free the memory.
94  */
videobuf_pages_to_sg(struct page ** pages,int nr_pages,int offset,size_t size)95 static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
96 					int nr_pages, int offset, size_t size)
97 {
98 	struct scatterlist *sglist;
99 	int i;
100 
101 	if (NULL == pages[0])
102 		return NULL;
103 	sglist = vmalloc(array_size(nr_pages, sizeof(*sglist)));
104 	if (NULL == sglist)
105 		return NULL;
106 	sg_init_table(sglist, nr_pages);
107 
108 	if (PageHighMem(pages[0]))
109 		/* DMA to highmem pages might not work */
110 		goto highmem;
111 	sg_set_page(&sglist[0], pages[0],
112 			min_t(size_t, PAGE_SIZE - offset, size), offset);
113 	size -= min_t(size_t, PAGE_SIZE - offset, size);
114 	for (i = 1; i < nr_pages; i++) {
115 		if (NULL == pages[i])
116 			goto nopage;
117 		if (PageHighMem(pages[i]))
118 			goto highmem;
119 		sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
120 		size -= min_t(size_t, PAGE_SIZE, size);
121 	}
122 	return sglist;
123 
124 nopage:
125 	dprintk(2, "sgl: oops - no page\n");
126 	vfree(sglist);
127 	return NULL;
128 
129 highmem:
130 	dprintk(2, "sgl: oops - highmem page\n");
131 	vfree(sglist);
132 	return NULL;
133 }
134 
135 /* --------------------------------------------------------------------- */
136 
videobuf_to_dma(struct videobuf_buffer * buf)137 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
138 {
139 	struct videobuf_dma_sg_memory *mem = buf->priv;
140 	BUG_ON(!mem);
141 
142 	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
143 
144 	return &mem->dma;
145 }
146 EXPORT_SYMBOL_GPL(videobuf_to_dma);
147 
videobuf_dma_init(struct videobuf_dmabuf * dma)148 static void videobuf_dma_init(struct videobuf_dmabuf *dma)
149 {
150 	memset(dma, 0, sizeof(*dma));
151 	dma->magic = MAGIC_DMABUF;
152 }
153 
videobuf_dma_init_user_locked(struct videobuf_dmabuf * dma,int direction,unsigned long data,unsigned long size)154 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
155 			int direction, unsigned long data, unsigned long size)
156 {
157 	unsigned long first, last;
158 	int err, rw = 0;
159 	unsigned int flags = FOLL_FORCE;
160 
161 	dma->direction = direction;
162 	switch (dma->direction) {
163 	case DMA_FROM_DEVICE:
164 		rw = READ;
165 		break;
166 	case DMA_TO_DEVICE:
167 		rw = WRITE;
168 		break;
169 	default:
170 		BUG();
171 	}
172 
173 	first = (data          & PAGE_MASK) >> PAGE_SHIFT;
174 	last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
175 	dma->offset = data & ~PAGE_MASK;
176 	dma->size = size;
177 	dma->nr_pages = last-first+1;
178 	dma->pages = kmalloc_array(dma->nr_pages, sizeof(struct page *),
179 				   GFP_KERNEL);
180 	if (NULL == dma->pages)
181 		return -ENOMEM;
182 
183 	if (rw == READ)
184 		flags |= FOLL_WRITE;
185 
186 	dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
187 		data, size, dma->nr_pages);
188 
189 	err = get_user_pages_longterm(data & PAGE_MASK, dma->nr_pages,
190 			     flags, dma->pages, NULL);
191 
192 	if (err != dma->nr_pages) {
193 		dma->nr_pages = (err >= 0) ? err : 0;
194 		dprintk(1, "get_user_pages_longterm: err=%d [%d]\n", err,
195 			dma->nr_pages);
196 		return err < 0 ? err : -EINVAL;
197 	}
198 	return 0;
199 }
200 
videobuf_dma_init_user(struct videobuf_dmabuf * dma,int direction,unsigned long data,unsigned long size)201 static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
202 			   unsigned long data, unsigned long size)
203 {
204 	int ret;
205 
206 	down_read(&current->mm->mmap_sem);
207 	ret = videobuf_dma_init_user_locked(dma, direction, data, size);
208 	up_read(&current->mm->mmap_sem);
209 
210 	return ret;
211 }
212 
videobuf_dma_init_kernel(struct videobuf_dmabuf * dma,int direction,int nr_pages)213 static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
214 			     int nr_pages)
215 {
216 	int i;
217 
218 	dprintk(1, "init kernel [%d pages]\n", nr_pages);
219 
220 	dma->direction = direction;
221 	dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
222 				   GFP_KERNEL);
223 	if (!dma->vaddr_pages)
224 		return -ENOMEM;
225 
226 	dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
227 	if (!dma->dma_addr) {
228 		kfree(dma->vaddr_pages);
229 		return -ENOMEM;
230 	}
231 	for (i = 0; i < nr_pages; i++) {
232 		void *addr;
233 
234 		addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
235 					  &(dma->dma_addr[i]), GFP_KERNEL);
236 		if (addr == NULL)
237 			goto out_free_pages;
238 
239 		dma->vaddr_pages[i] = virt_to_page(addr);
240 	}
241 	dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
242 			  PAGE_KERNEL);
243 	if (NULL == dma->vaddr) {
244 		dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
245 		goto out_free_pages;
246 	}
247 
248 	dprintk(1, "vmalloc is at addr %p, size=%d\n",
249 		dma->vaddr, nr_pages << PAGE_SHIFT);
250 
251 	memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
252 	dma->nr_pages = nr_pages;
253 
254 	return 0;
255 out_free_pages:
256 	while (i > 0) {
257 		void *addr;
258 
259 		i--;
260 		addr = page_address(dma->vaddr_pages[i]);
261 		dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]);
262 	}
263 	kfree(dma->dma_addr);
264 	dma->dma_addr = NULL;
265 	kfree(dma->vaddr_pages);
266 	dma->vaddr_pages = NULL;
267 
268 	return -ENOMEM;
269 
270 }
271 
videobuf_dma_init_overlay(struct videobuf_dmabuf * dma,int direction,dma_addr_t addr,int nr_pages)272 static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
273 			      dma_addr_t addr, int nr_pages)
274 {
275 	dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
276 		nr_pages, (unsigned long)addr);
277 	dma->direction = direction;
278 
279 	if (0 == addr)
280 		return -EINVAL;
281 
282 	dma->bus_addr = addr;
283 	dma->nr_pages = nr_pages;
284 
285 	return 0;
286 }
287 
videobuf_dma_map(struct device * dev,struct videobuf_dmabuf * dma)288 static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
289 {
290 	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
291 	BUG_ON(0 == dma->nr_pages);
292 
293 	if (dma->pages) {
294 		dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
295 						   dma->offset, dma->size);
296 	}
297 	if (dma->vaddr) {
298 		dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
299 						     dma->nr_pages);
300 	}
301 	if (dma->bus_addr) {
302 		dma->sglist = vmalloc(sizeof(*dma->sglist));
303 		if (NULL != dma->sglist) {
304 			dma->sglen = 1;
305 			sg_dma_address(&dma->sglist[0])	= dma->bus_addr
306 							& PAGE_MASK;
307 			dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
308 			sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
309 		}
310 	}
311 	if (NULL == dma->sglist) {
312 		dprintk(1, "scatterlist is NULL\n");
313 		return -ENOMEM;
314 	}
315 	if (!dma->bus_addr) {
316 		dma->sglen = dma_map_sg(dev, dma->sglist,
317 					dma->nr_pages, dma->direction);
318 		if (0 == dma->sglen) {
319 			printk(KERN_WARNING
320 			       "%s: videobuf_map_sg failed\n", __func__);
321 			vfree(dma->sglist);
322 			dma->sglist = NULL;
323 			dma->sglen = 0;
324 			return -ENOMEM;
325 		}
326 	}
327 
328 	return 0;
329 }
330 
videobuf_dma_unmap(struct device * dev,struct videobuf_dmabuf * dma)331 int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
332 {
333 	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
334 
335 	if (!dma->sglen)
336 		return 0;
337 
338 	dma_unmap_sg(dev, dma->sglist, dma->nr_pages, dma->direction);
339 
340 	vfree(dma->sglist);
341 	dma->sglist = NULL;
342 	dma->sglen = 0;
343 
344 	return 0;
345 }
346 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
347 
videobuf_dma_free(struct videobuf_dmabuf * dma)348 int videobuf_dma_free(struct videobuf_dmabuf *dma)
349 {
350 	int i;
351 	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
352 	BUG_ON(dma->sglen);
353 
354 	if (dma->pages) {
355 		for (i = 0; i < dma->nr_pages; i++) {
356 			if (dma->direction == DMA_FROM_DEVICE)
357 				set_page_dirty_lock(dma->pages[i]);
358 			put_page(dma->pages[i]);
359 		}
360 		kfree(dma->pages);
361 		dma->pages = NULL;
362 	}
363 
364 	if (dma->dma_addr) {
365 		for (i = 0; i < dma->nr_pages; i++) {
366 			void *addr;
367 
368 			addr = page_address(dma->vaddr_pages[i]);
369 			dma_free_coherent(dma->dev, PAGE_SIZE, addr,
370 					  dma->dma_addr[i]);
371 		}
372 		kfree(dma->dma_addr);
373 		dma->dma_addr = NULL;
374 		kfree(dma->vaddr_pages);
375 		dma->vaddr_pages = NULL;
376 		vunmap(dma->vaddr);
377 		dma->vaddr = NULL;
378 	}
379 
380 	if (dma->bus_addr)
381 		dma->bus_addr = 0;
382 	dma->direction = DMA_NONE;
383 
384 	return 0;
385 }
386 EXPORT_SYMBOL_GPL(videobuf_dma_free);
387 
388 /* --------------------------------------------------------------------- */
389 
videobuf_vm_open(struct vm_area_struct * vma)390 static void videobuf_vm_open(struct vm_area_struct *vma)
391 {
392 	struct videobuf_mapping *map = vma->vm_private_data;
393 
394 	dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
395 		map->count, vma->vm_start, vma->vm_end);
396 
397 	map->count++;
398 }
399 
videobuf_vm_close(struct vm_area_struct * vma)400 static void videobuf_vm_close(struct vm_area_struct *vma)
401 {
402 	struct videobuf_mapping *map = vma->vm_private_data;
403 	struct videobuf_queue *q = map->q;
404 	struct videobuf_dma_sg_memory *mem;
405 	int i;
406 
407 	dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
408 		map->count, vma->vm_start, vma->vm_end);
409 
410 	map->count--;
411 	if (0 == map->count) {
412 		dprintk(1, "munmap %p q=%p\n", map, q);
413 		videobuf_queue_lock(q);
414 		for (i = 0; i < VIDEO_MAX_FRAME; i++) {
415 			if (NULL == q->bufs[i])
416 				continue;
417 			mem = q->bufs[i]->priv;
418 			if (!mem)
419 				continue;
420 
421 			MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
422 
423 			if (q->bufs[i]->map != map)
424 				continue;
425 			q->bufs[i]->map   = NULL;
426 			q->bufs[i]->baddr = 0;
427 			q->ops->buf_release(q, q->bufs[i]);
428 		}
429 		videobuf_queue_unlock(q);
430 		kfree(map);
431 	}
432 	return;
433 }
434 
435 /*
436  * Get a anonymous page for the mapping.  Make sure we can DMA to that
437  * memory location with 32bit PCI devices (i.e. don't use highmem for
438  * now ...).  Bounce buffers don't work very well for the data rates
439  * video capture has.
440  */
videobuf_vm_fault(struct vm_fault * vmf)441 static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf)
442 {
443 	struct vm_area_struct *vma = vmf->vma;
444 	struct page *page;
445 
446 	dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
447 		vmf->address, vma->vm_start, vma->vm_end);
448 
449 	page = alloc_page(GFP_USER | __GFP_DMA32);
450 	if (!page)
451 		return VM_FAULT_OOM;
452 	clear_user_highpage(page, vmf->address);
453 	vmf->page = page;
454 
455 	return 0;
456 }
457 
458 static const struct vm_operations_struct videobuf_vm_ops = {
459 	.open	= videobuf_vm_open,
460 	.close	= videobuf_vm_close,
461 	.fault	= videobuf_vm_fault,
462 };
463 
464 /* ---------------------------------------------------------------------
465  * SG handlers for the generic methods
466  */
467 
468 /* Allocated area consists on 3 parts:
469 	struct video_buffer
470 	struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
471 	struct videobuf_dma_sg_memory
472  */
473 
__videobuf_alloc_vb(size_t size)474 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
475 {
476 	struct videobuf_dma_sg_memory *mem;
477 	struct videobuf_buffer *vb;
478 
479 	vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
480 	if (!vb)
481 		return vb;
482 
483 	mem = vb->priv = ((char *)vb) + size;
484 	mem->magic = MAGIC_SG_MEM;
485 
486 	videobuf_dma_init(&mem->dma);
487 
488 	dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
489 		__func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
490 		mem, (long)sizeof(*mem));
491 
492 	return vb;
493 }
494 
__videobuf_to_vaddr(struct videobuf_buffer * buf)495 static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
496 {
497 	struct videobuf_dma_sg_memory *mem = buf->priv;
498 	BUG_ON(!mem);
499 
500 	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
501 
502 	return mem->dma.vaddr;
503 }
504 
__videobuf_iolock(struct videobuf_queue * q,struct videobuf_buffer * vb,struct v4l2_framebuffer * fbuf)505 static int __videobuf_iolock(struct videobuf_queue *q,
506 			     struct videobuf_buffer *vb,
507 			     struct v4l2_framebuffer *fbuf)
508 {
509 	int err, pages;
510 	dma_addr_t bus;
511 	struct videobuf_dma_sg_memory *mem = vb->priv;
512 	BUG_ON(!mem);
513 
514 	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
515 
516 	if (!mem->dma.dev)
517 		mem->dma.dev = q->dev;
518 	else
519 		WARN_ON(mem->dma.dev != q->dev);
520 
521 	switch (vb->memory) {
522 	case V4L2_MEMORY_MMAP:
523 	case V4L2_MEMORY_USERPTR:
524 		if (0 == vb->baddr) {
525 			/* no userspace addr -- kernel bounce buffer */
526 			pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
527 			err = videobuf_dma_init_kernel(&mem->dma,
528 						       DMA_FROM_DEVICE,
529 						       pages);
530 			if (0 != err)
531 				return err;
532 		} else if (vb->memory == V4L2_MEMORY_USERPTR) {
533 			/* dma directly to userspace */
534 			err = videobuf_dma_init_user(&mem->dma,
535 						     DMA_FROM_DEVICE,
536 						     vb->baddr, vb->bsize);
537 			if (0 != err)
538 				return err;
539 		} else {
540 			/* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
541 			buffers can only be called from videobuf_qbuf
542 			we take current->mm->mmap_sem there, to prevent
543 			locking inversion, so don't take it here */
544 
545 			err = videobuf_dma_init_user_locked(&mem->dma,
546 						      DMA_FROM_DEVICE,
547 						      vb->baddr, vb->bsize);
548 			if (0 != err)
549 				return err;
550 		}
551 		break;
552 	case V4L2_MEMORY_OVERLAY:
553 		if (NULL == fbuf)
554 			return -EINVAL;
555 		/* FIXME: need sanity checks for vb->boff */
556 		/*
557 		 * Using a double cast to avoid compiler warnings when
558 		 * building for PAE. Compiler doesn't like direct casting
559 		 * of a 32 bit ptr to 64 bit integer.
560 		 */
561 		bus   = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
562 		pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
563 		err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
564 						bus, pages);
565 		if (0 != err)
566 			return err;
567 		break;
568 	default:
569 		BUG();
570 	}
571 	err = videobuf_dma_map(q->dev, &mem->dma);
572 	if (0 != err)
573 		return err;
574 
575 	return 0;
576 }
577 
__videobuf_sync(struct videobuf_queue * q,struct videobuf_buffer * buf)578 static int __videobuf_sync(struct videobuf_queue *q,
579 			   struct videobuf_buffer *buf)
580 {
581 	struct videobuf_dma_sg_memory *mem = buf->priv;
582 	BUG_ON(!mem || !mem->dma.sglen);
583 
584 	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
585 	MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
586 
587 	dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
588 			    mem->dma.nr_pages, mem->dma.direction);
589 
590 	return 0;
591 }
592 
__videobuf_mmap_mapper(struct videobuf_queue * q,struct videobuf_buffer * buf,struct vm_area_struct * vma)593 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
594 				  struct videobuf_buffer *buf,
595 				  struct vm_area_struct *vma)
596 {
597 	struct videobuf_dma_sg_memory *mem = buf->priv;
598 	struct videobuf_mapping *map;
599 	unsigned int first, last, size = 0, i;
600 	int retval;
601 
602 	retval = -EINVAL;
603 
604 	BUG_ON(!mem);
605 	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
606 
607 	/* look for first buffer to map */
608 	for (first = 0; first < VIDEO_MAX_FRAME; first++) {
609 		if (buf == q->bufs[first]) {
610 			size = PAGE_ALIGN(q->bufs[first]->bsize);
611 			break;
612 		}
613 	}
614 
615 	/* paranoia, should never happen since buf is always valid. */
616 	if (!size) {
617 		dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
618 				(vma->vm_pgoff << PAGE_SHIFT));
619 		goto done;
620 	}
621 
622 	last = first;
623 
624 	/* create mapping + update buffer list */
625 	retval = -ENOMEM;
626 	map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
627 	if (NULL == map)
628 		goto done;
629 
630 	size = 0;
631 	for (i = first; i <= last; i++) {
632 		if (NULL == q->bufs[i])
633 			continue;
634 		q->bufs[i]->map   = map;
635 		q->bufs[i]->baddr = vma->vm_start + size;
636 		size += PAGE_ALIGN(q->bufs[i]->bsize);
637 	}
638 
639 	map->count    = 1;
640 	map->q        = q;
641 	vma->vm_ops   = &videobuf_vm_ops;
642 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
643 	vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
644 	vma->vm_private_data = map;
645 	dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
646 		map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
647 	retval = 0;
648 
649 done:
650 	return retval;
651 }
652 
653 static struct videobuf_qtype_ops sg_ops = {
654 	.magic        = MAGIC_QTYPE_OPS,
655 
656 	.alloc_vb     = __videobuf_alloc_vb,
657 	.iolock       = __videobuf_iolock,
658 	.sync         = __videobuf_sync,
659 	.mmap_mapper  = __videobuf_mmap_mapper,
660 	.vaddr        = __videobuf_to_vaddr,
661 };
662 
videobuf_sg_alloc(size_t size)663 void *videobuf_sg_alloc(size_t size)
664 {
665 	struct videobuf_queue q;
666 
667 	/* Required to make generic handler to call __videobuf_alloc */
668 	q.int_ops = &sg_ops;
669 
670 	q.msize = size;
671 
672 	return videobuf_alloc_vb(&q);
673 }
674 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
675 
videobuf_queue_sg_init(struct videobuf_queue * q,const struct videobuf_queue_ops * ops,struct device * dev,spinlock_t * irqlock,enum v4l2_buf_type type,enum v4l2_field field,unsigned int msize,void * priv,struct mutex * ext_lock)676 void videobuf_queue_sg_init(struct videobuf_queue *q,
677 			 const struct videobuf_queue_ops *ops,
678 			 struct device *dev,
679 			 spinlock_t *irqlock,
680 			 enum v4l2_buf_type type,
681 			 enum v4l2_field field,
682 			 unsigned int msize,
683 			 void *priv,
684 			 struct mutex *ext_lock)
685 {
686 	videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
687 				 priv, &sg_ops, ext_lock);
688 }
689 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
690 
691