videobuf-dma-sg.c 17.9 KB
Newer Older
1
/*
2
 * helper functions for SG DMA video4linux capture buffers
3
 *
4
 * The functions expect the hardware being able to scatter gather
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * (i.e. the buffers are not linear in physical memory, but fragmented
 * into PAGE_SIZE chunks).  They also assume the driver does not need
 * to touch the video data.
 *
 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
 *
 * Highly based on video-buf written originally by:
 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
 * (c) 2006 Ted Walther and John Sokol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
24
#include <linux/sched.h>
25 26 27
#include <linux/slab.h>
#include <linux/interrupt.h>

28
#include <linux/dma-mapping.h>
29 30
#include <linux/vmalloc.h>
#include <linux/pagemap.h>
31
#include <linux/scatterlist.h>
32 33 34 35 36 37 38 39
#include <asm/page.h>
#include <asm/pgtable.h>

#include <media/videobuf-dma-sg.h>

#define MAGIC_DMABUF 0x19721112
#define MAGIC_SG_MEM 0x17890714

40 41 42 43 44 45
#define MAGIC_CHECK(is, should)						\
	if (unlikely((is) != (should))) {				\
		printk(KERN_ERR "magic mismatch: %x (expected %x)\n",	\
				is, should);				\
		BUG();							\
	}
46

47
static int debug;
48 49
module_param(debug, int, 0644);

50
MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
51 52 53
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");

54 55 56
#define dprintk(level, fmt, arg...)					\
	if (debug >= level)						\
		printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
57 58 59

/* --------------------------------------------------------------------- */

60
struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
61 62 63 64 65
{
	struct scatterlist *sglist;
	struct page *pg;
	int i;

66
	sglist = vmalloc(nr_pages * sizeof(*sglist));
67 68
	if (NULL == sglist)
		return NULL;
69
	memset(sglist, 0, nr_pages * sizeof(*sglist));
J
Jens Axboe 已提交
70
	sg_init_table(sglist, nr_pages);
71 72 73 74 75
	for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
		pg = vmalloc_to_page(virt);
		if (NULL == pg)
			goto err;
		BUG_ON(PageHighMem(pg));
76
		sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
77 78 79
	}
	return sglist;

80
err:
81
	vfree(sglist);
82 83
	return NULL;
}
84
EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
85

86 87
struct scatterlist *videobuf_pages_to_sg(struct page **pages, int nr_pages,
					 int offset)
88 89
{
	struct scatterlist *sglist;
90
	int i;
91 92 93

	if (NULL == pages[0])
		return NULL;
94
	sglist = vmalloc(nr_pages * sizeof(*sglist));
95 96
	if (NULL == sglist)
		return NULL;
J
Jens Axboe 已提交
97
	sg_init_table(sglist, nr_pages);
98 99 100 101

	if (PageHighMem(pages[0]))
		/* DMA to highmem pages might not work */
		goto highmem;
102
	sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
103 104 105 106 107
	for (i = 1; i < nr_pages; i++) {
		if (NULL == pages[i])
			goto nopage;
		if (PageHighMem(pages[i]))
			goto highmem;
108
		sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
109 110 111
	}
	return sglist;

112 113
nopage:
	dprintk(2, "sgl: oops - no page\n");
114
	vfree(sglist);
115 116
	return NULL;

117 118
highmem:
	dprintk(2, "sgl: oops - highmem page\n");
119
	vfree(sglist);
120 121 122 123 124
	return NULL;
}

/* --------------------------------------------------------------------- */

125
struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
126
{
127 128
	struct videobuf_dma_sg_memory *mem = buf->priv;
	BUG_ON(!mem);
129

130
	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
131 132 133

	return &mem->dma;
}
134
EXPORT_SYMBOL_GPL(videobuf_to_dma);
135 136 137

void videobuf_dma_init(struct videobuf_dmabuf *dma)
{
138
	memset(dma, 0, sizeof(*dma));
139 140
	dma->magic = MAGIC_DMABUF;
}
141
EXPORT_SYMBOL_GPL(videobuf_dma_init);
142

143 144
static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
			int direction, unsigned long data, unsigned long size)
145
{
146
	unsigned long first, last;
147 148 149 150
	int err, rw = 0;

	dma->direction = direction;
	switch (dma->direction) {
151 152 153 154 155 156 157 158
	case DMA_FROM_DEVICE:
		rw = READ;
		break;
	case DMA_TO_DEVICE:
		rw = WRITE;
		break;
	default:
		BUG();
159 160 161 162 163 164
	}

	first = (data          & PAGE_MASK) >> PAGE_SHIFT;
	last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
	dma->offset   = data & ~PAGE_MASK;
	dma->nr_pages = last-first+1;
165
	dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
166 167 168
	if (NULL == dma->pages)
		return -ENOMEM;

169 170 171 172
	dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
		data, size, dma->nr_pages);

	err = get_user_pages(current, current->mm,
173 174 175
			     data & PAGE_MASK, dma->nr_pages,
			     rw == READ, 1, /* force */
			     dma->pages, NULL);
176

177 178
	if (err != dma->nr_pages) {
		dma->nr_pages = (err >= 0) ? err : 0;
179
		dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
180 181 182 183 184
		return err < 0 ? err : -EINVAL;
	}
	return 0;
}

185 186 187 188
int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
			   unsigned long data, unsigned long size)
{
	int ret;
189

190 191 192 193 194 195
	down_read(&current->mm->mmap_sem);
	ret = videobuf_dma_init_user_locked(dma, direction, data, size);
	up_read(&current->mm->mmap_sem);

	return ret;
}
196
EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
197

198 199 200
int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
			     int nr_pages)
{
201 202
	dprintk(1, "init kernel [%d pages]\n", nr_pages);

203 204 205
	dma->direction = direction;
	dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
	if (NULL == dma->vmalloc) {
206
		dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
207 208
		return -ENOMEM;
	}
209 210

	dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
211 212
				(unsigned long)dma->vmalloc,
				nr_pages << PAGE_SHIFT);
213 214

	memset(dma->vmalloc, 0, nr_pages << PAGE_SHIFT);
215
	dma->nr_pages = nr_pages;
216

217 218
	return 0;
}
219
EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
220 221 222 223

int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
			      dma_addr_t addr, int nr_pages)
{
224 225
	dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
		nr_pages, (unsigned long)addr);
226
	dma->direction = direction;
227

228 229 230 231 232
	if (0 == addr)
		return -EINVAL;

	dma->bus_addr = addr;
	dma->nr_pages = nr_pages;
233

234 235
	return 0;
}
236
EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
237

238
int videobuf_dma_map(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
239
{
240
	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
241 242 243 244 245 246 247
	BUG_ON(0 == dma->nr_pages);

	if (dma->pages) {
		dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
						   dma->offset);
	}
	if (dma->vmalloc) {
248 249
		dma->sglist = videobuf_vmalloc_to_sg(dma->vmalloc,
						     dma->nr_pages);
250 251
	}
	if (dma->bus_addr) {
252
		dma->sglist = vmalloc(sizeof(*dma->sglist));
253
		if (NULL != dma->sglist) {
254 255 256 257 258
			dma->sglen = 1;
			sg_dma_address(&dma->sglist[0])	= dma->bus_addr
							& PAGE_MASK;
			dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
			sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
259 260 261
		}
	}
	if (NULL == dma->sglist) {
262
		dprintk(1, "scatterlist is NULL\n");
263 264 265
		return -ENOMEM;
	}
	if (!dma->bus_addr) {
266
		dma->sglen = dma_map_sg(q->dev, dma->sglist,
267 268 269
					dma->nr_pages, dma->direction);
		if (0 == dma->sglen) {
			printk(KERN_WARNING
270
			       "%s: videobuf_map_sg failed\n", __func__);
271
			vfree(dma->sglist);
272 273
			dma->sglist = NULL;
			dma->sglen = 0;
274
			return -ENOMEM;
275 276
		}
	}
277

278 279
	return 0;
}
280
EXPORT_SYMBOL_GPL(videobuf_dma_map);
281

282
int videobuf_dma_sync(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
283
{
284
	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
285 286
	BUG_ON(!dma->sglen);

287
	dma_sync_sg_for_cpu(q->dev, dma->sglist, dma->nr_pages, dma->direction);
288

289 290
	return 0;
}
291
EXPORT_SYMBOL_GPL(videobuf_dma_sync);
292

293
int videobuf_dma_unmap(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
294
{
295
	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
296

297 298 299
	if (!dma->sglen)
		return 0;

300
	dma_unmap_sg(q->dev, dma->sglist, dma->nr_pages, dma->direction);
301

302
	vfree(dma->sglist);
303 304
	dma->sglist = NULL;
	dma->sglen = 0;
305

306 307
	return 0;
}
308
EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
309 310 311

int videobuf_dma_free(struct videobuf_dmabuf *dma)
{
312 313
	int i;
	MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
314 315 316
	BUG_ON(dma->sglen);

	if (dma->pages) {
317
		for (i = 0; i < dma->nr_pages; i++)
318 319 320 321 322 323 324 325
			page_cache_release(dma->pages[i]);
		kfree(dma->pages);
		dma->pages = NULL;
	}

	vfree(dma->vmalloc);
	dma->vmalloc = NULL;

326
	if (dma->bus_addr)
327
		dma->bus_addr = 0;
328
	dma->direction = DMA_NONE;
329

330 331
	return 0;
}
332
EXPORT_SYMBOL_GPL(videobuf_dma_free);
333 334 335

/* --------------------------------------------------------------------- */

336
int videobuf_sg_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
337 338 339
{
	struct videobuf_queue q;

340
	q.dev = dev;
341

342
	return videobuf_dma_map(&q, dma);
343
}
344
EXPORT_SYMBOL_GPL(videobuf_sg_dma_map);
345

346
int videobuf_sg_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
347 348 349
{
	struct videobuf_queue q;

350
	q.dev = dev;
351

352
	return videobuf_dma_unmap(&q, dma);
353
}
354
EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap);
355 356 357

/* --------------------------------------------------------------------- */

358
static void videobuf_vm_open(struct vm_area_struct *vma)
359 360 361
{
	struct videobuf_mapping *map = vma->vm_private_data;

362 363 364
	dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
		map->count, vma->vm_start, vma->vm_end);

365 366 367
	map->count++;
}

368
static void videobuf_vm_close(struct vm_area_struct *vma)
369 370 371
{
	struct videobuf_mapping *map = vma->vm_private_data;
	struct videobuf_queue *q = map->q;
372
	struct videobuf_dma_sg_memory *mem;
373 374
	int i;

375 376
	dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
		map->count, vma->vm_start, vma->vm_end);
377 378 379

	map->count--;
	if (0 == map->count) {
380
		dprintk(1, "munmap %p q=%p\n", map, q);
381
		mutex_lock(&q->vb_lock);
382 383 384
		for (i = 0; i < VIDEO_MAX_FRAME; i++) {
			if (NULL == q->bufs[i])
				continue;
385
			mem = q->bufs[i]->priv;
386 387 388
			if (!mem)
				continue;

389
			MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
390

391
			if (q->bufs[i]->map != map)
392
				continue;
393
			q->bufs[i]->map   = NULL;
394
			q->bufs[i]->baddr = 0;
395
			q->ops->buf_release(q, q->bufs[i]);
396
		}
397
		mutex_unlock(&q->vb_lock);
398 399 400 401 402 403 404 405 406 407 408
		kfree(map);
	}
	return;
}

/*
 * Get a anonymous page for the mapping.  Make sure we can DMA to that
 * memory location with 32bit PCI devices (i.e. don't use highmem for
 * now ...).  Bounce buffers don't work very well for the data rates
 * video capture has.
 */
409
static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
410 411 412
{
	struct page *page;

413 414 415 416
	dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
		(unsigned long)vmf->virtual_address,
		vma->vm_start, vma->vm_end);

417 418
	page = alloc_page(GFP_USER | __GFP_DMA32);
	if (!page)
419
		return VM_FAULT_OOM;
420
	clear_user_highpage(page, (unsigned long)vmf->virtual_address);
421
	vmf->page = page;
422

423
	return 0;
424 425
}

426 427 428 429
static const struct vm_operations_struct videobuf_vm_ops = {
	.open	= videobuf_vm_open,
	.close	= videobuf_vm_close,
	.fault	= videobuf_vm_fault,
430 431 432
};

/* ---------------------------------------------------------------------
433
 * SG handlers for the generic methods
434 435 436 437 438
 */

/* Allocated area consists on 3 parts:
	struct video_buffer
	struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
439
	struct videobuf_dma_sg_memory
440 441 442 443
 */

static void *__videobuf_alloc(size_t size)
{
444
	struct videobuf_dma_sg_memory *mem;
445 446
	struct videobuf_buffer *vb;

447
	vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
448 449
	if (!vb)
		return vb;
450

451 452
	mem = vb->priv = ((char *)vb) + size;
	mem->magic = MAGIC_SG_MEM;
453 454 455

	videobuf_dma_init(&mem->dma);

456 457 458
	dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
		__func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
		mem, (long)sizeof(*mem));
459 460 461 462

	return vb;
}

463
static void *__videobuf_to_vmalloc(struct videobuf_buffer *buf)
464 465 466 467 468 469 470 471 472
{
	struct videobuf_dma_sg_memory *mem = buf->priv;
	BUG_ON(!mem);

	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);

	return mem->dma.vmalloc;
}

473 474 475
static int __videobuf_iolock(struct videobuf_queue *q,
			     struct videobuf_buffer *vb,
			     struct v4l2_framebuffer *fbuf)
476
{
477
	int err, pages;
478
	dma_addr_t bus;
479
	struct videobuf_dma_sg_memory *mem = vb->priv;
480 481
	BUG_ON(!mem);

482
	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
483 484 485 486 487 488 489

	switch (vb->memory) {
	case V4L2_MEMORY_MMAP:
	case V4L2_MEMORY_USERPTR:
		if (0 == vb->baddr) {
			/* no userspace addr -- kernel bounce buffer */
			pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
490 491 492
			err = videobuf_dma_init_kernel(&mem->dma,
						       DMA_FROM_DEVICE,
						       pages);
493 494
			if (0 != err)
				return err;
495
		} else if (vb->memory == V4L2_MEMORY_USERPTR) {
496
			/* dma directly to userspace */
497 498 499
			err = videobuf_dma_init_user(&mem->dma,
						     DMA_FROM_DEVICE,
						     vb->baddr, vb->bsize);
500 501
			if (0 != err)
				return err;
502 503 504 505 506 507 508
		} else {
			/* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
			buffers can only be called from videobuf_qbuf
			we take current->mm->mmap_sem there, to prevent
			locking inversion, so don't take it here */

			err = videobuf_dma_init_user_locked(&mem->dma,
509
						      DMA_FROM_DEVICE,
510 511 512
						      vb->baddr, vb->bsize);
			if (0 != err)
				return err;
513 514 515 516 517 518 519 520 521 522 523 524 525
		}
		break;
	case V4L2_MEMORY_OVERLAY:
		if (NULL == fbuf)
			return -EINVAL;
		/* FIXME: need sanity checks for vb->boff */
		/*
		 * Using a double cast to avoid compiler warnings when
		 * building for PAE. Compiler doesn't like direct casting
		 * of a 32 bit ptr to 64 bit integer.
		 */
		bus   = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
		pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
526
		err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
527 528 529 530 531 532 533
						bus, pages);
		if (0 != err)
			return err;
		break;
	default:
		BUG();
	}
534
	err = videobuf_dma_map(q, &mem->dma);
535 536 537 538 539 540 541 542 543
	if (0 != err)
		return err;

	return 0;
}

static int __videobuf_sync(struct videobuf_queue *q,
			   struct videobuf_buffer *buf)
{
544 545
	struct videobuf_dma_sg_memory *mem = buf->priv;
	BUG_ON(!mem);
546
	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
547

548
	return videobuf_dma_sync(q, &mem->dma);
549 550 551 552 553
}

static int __videobuf_mmap_mapper(struct videobuf_queue *q,
			 struct vm_area_struct *vma)
{
554
	struct videobuf_dma_sg_memory *mem;
555
	struct videobuf_mapping *map;
556
	unsigned int first, last, size, i;
557 558 559 560
	int retval;

	retval = -EINVAL;
	if (!(vma->vm_flags & VM_WRITE)) {
561
		dprintk(1, "mmap app bug: PROT_WRITE please\n");
562 563 564
		goto done;
	}
	if (!(vma->vm_flags & VM_SHARED)) {
565
		dprintk(1, "mmap app bug: MAP_SHARED please\n");
566 567 568
		goto done;
	}

569 570 571 572 573 574 575 576
	/* This function maintains backwards compatibility with V4L1 and will
	 * map more than one buffer if the vma length is equal to the combined
	 * size of multiple buffers than it will map them together.  See
	 * VIDIOCGMBUF in the v4l spec
	 *
	 * TODO: Allow drivers to specify if they support this mode
	 */

577 578 579 580
	/* look for first buffer to map */
	for (first = 0; first < VIDEO_MAX_FRAME; first++) {
		if (NULL == q->bufs[first])
			continue;
581
		mem = q->bufs[first]->priv;
582
		BUG_ON(!mem);
583
		MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
584 585 586 587 588 589 590

		if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
			continue;
		if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
			break;
	}
	if (VIDEO_MAX_FRAME == first) {
591
		dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
592 593 594 595 596 597 598 599 600 601
			(vma->vm_pgoff << PAGE_SHIFT));
		goto done;
	}

	/* look for last buffer to map */
	for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
		if (NULL == q->bufs[last])
			continue;
		if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
			continue;
602
		if (q->bufs[last]->map) {
603 604 605
			retval = -EBUSY;
			goto done;
		}
606
		size += PAGE_ALIGN(q->bufs[last]->bsize);
607 608 609 610
		if (size == (vma->vm_end - vma->vm_start))
			break;
	}
	if (VIDEO_MAX_FRAME == last) {
611
		dprintk(1, "mmap app bug: size invalid [size=0x%lx]\n",
612 613 614 615 616 617
			(vma->vm_end - vma->vm_start));
		goto done;
	}

	/* create mapping + update buffer list */
	retval = -ENOMEM;
618
	map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
619 620
	if (NULL == map)
		goto done;
621 622 623 624 625

	size = 0;
	for (i = first; i <= last; i++) {
		if (NULL == q->bufs[i])
			continue;
626
		q->bufs[i]->map   = map;
627
		q->bufs[i]->baddr = vma->vm_start + size;
628
		size += PAGE_ALIGN(q->bufs[i]->bsize);
629
	}
630

631 632 633 634 635 636 637 638
	map->count    = 1;
	map->start    = vma->vm_start;
	map->end      = vma->vm_end;
	map->q        = q;
	vma->vm_ops   = &videobuf_vm_ops;
	vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
	vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
	vma->vm_private_data = map;
639 640
	dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
		map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
641 642
	retval = 0;

643
done:
644 645 646
	return retval;
}

647
static int __videobuf_copy_to_user(struct videobuf_queue *q,
648
				char __user *data, size_t count,
649
				int nonblocking)
650
{
651 652
	struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
	BUG_ON(!mem);
653
	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
654 655 656 657 658 659 660 661 662 663 664

	/* copy to userspace */
	if (count > q->read_buf->size - q->read_off)
		count = q->read_buf->size - q->read_off;

	if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
		return -EFAULT;

	return count;
}

665
static int __videobuf_copy_stream(struct videobuf_queue *q,
666
				char __user *data, size_t count, size_t pos,
667
				int vbihack, int nonblocking)
668
{
669
	unsigned int *fc;
670 671
	struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
	BUG_ON(!mem);
672
	MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
673 674 675

	if (vbihack) {
		/* dirty, undocumented hack -- pass the frame counter
676 677 678 679 680
		 * within the last four bytes of each vbi data block.
		 * We need that one to maintain backward compatibility
		 * to all vbi decoding software out there ... */
		fc  = (unsigned int *)mem->dma.vmalloc;
		fc += (q->read_buf->size >> 2) - 1;
681
		*fc = q->read_buf->field_count >> 1;
682
		dprintk(1, "vbihack: %d\n", *fc);
683 684 685
	}

	/* copy stuff using the common method */
686
	count = __videobuf_copy_to_user(q, data, count, nonblocking);
687

688
	if ((count == -EFAULT) && (0 == pos))
689 690 691 692 693
		return -EFAULT;

	return count;
}

694
static struct videobuf_qtype_ops sg_ops = {
695 696 697 698 699 700
	.magic        = MAGIC_QTYPE_OPS,

	.alloc        = __videobuf_alloc,
	.iolock       = __videobuf_iolock,
	.sync         = __videobuf_sync,
	.mmap_mapper  = __videobuf_mmap_mapper,
701
	.video_copy_to_user = __videobuf_copy_to_user,
702
	.copy_stream  = __videobuf_copy_stream,
703
	.vmalloc      = __videobuf_to_vmalloc,
704 705
};

706
void *videobuf_sg_alloc(size_t size)
707 708 709 710
{
	struct videobuf_queue q;

	/* Required to make generic handler to call __videobuf_alloc */
711
	q.int_ops = &sg_ops;
712

713
	q.msize = size;
714

715
	return videobuf_alloc(&q);
716
}
717
EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
718

719
void videobuf_queue_sg_init(struct videobuf_queue *q,
720
			 const struct videobuf_queue_ops *ops,
721
			 struct device *dev,
722 723 724 725 726 727
			 spinlock_t *irqlock,
			 enum v4l2_buf_type type,
			 enum v4l2_field field,
			 unsigned int msize,
			 void *priv)
{
728
	videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
729
				 priv, &sg_ops);
730
}
731
EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
732