PageRenderTime 94ms CodeModel.GetById 9ms app.highlight 77ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/videobuf-core.c

https://gitlab.com/TeamCarbonXtreme/android_kernel_samsung_msm7x27
C | 1197 lines | 955 code | 182 blank | 60 comment | 211 complexity | f007daf5d537529b8443e695462646de MD5 | raw file
   1/*
   2 * generic helper functions for handling video4linux capture buffers
   3 *
   4 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
   5 *
   6 * Highly based on video-buf written originally by:
   7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
   8 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
   9 * (c) 2006 Ted Walther and John Sokol
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2
  14 */
  15
  16#include <linux/init.h>
  17#include <linux/module.h>
  18#include <linux/moduleparam.h>
  19#include <linux/mm.h>
  20#include <linux/sched.h>
  21#include <linux/slab.h>
  22#include <linux/interrupt.h>
  23
  24#include <media/videobuf-core.h>
  25
  26#define MAGIC_BUFFER 0x20070728
  27#define MAGIC_CHECK(is, should)						\
  28	do {								\
  29		if (unlikely((is) != (should))) {			\
  30			printk(KERN_ERR					\
  31				"magic mismatch: %x (expected %x)\n",	\
  32					is, should);			\
  33			BUG();						\
  34		}							\
  35	} while (0)
  36
  37static int debug;
  38module_param(debug, int, 0644);
  39
  40MODULE_DESCRIPTION("helper module to manage video4linux buffers");
  41MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  42MODULE_LICENSE("GPL");
  43
  44#define dprintk(level, fmt, arg...)					\
  45	do {								\
  46		if (debug >= level)					\
  47			printk(KERN_DEBUG "vbuf: " fmt, ## arg);	\
  48	} while (0)
  49
  50/* --------------------------------------------------------------------- */
  51
  52#define CALL(q, f, arg...)						\
  53	((q->int_ops->f) ? q->int_ops->f(arg) : 0)
  54
  55struct videobuf_buffer *videobuf_alloc(struct videobuf_queue *q)
  56{
  57	struct videobuf_buffer *vb;
  58
  59	BUG_ON(q->msize < sizeof(*vb));
  60
  61	if (!q->int_ops || !q->int_ops->alloc) {
  62		printk(KERN_ERR "No specific ops defined!\n");
  63		BUG();
  64	}
  65
  66	vb = q->int_ops->alloc(q->msize);
  67	if (NULL != vb) {
  68		init_waitqueue_head(&vb->done);
  69		vb->magic = MAGIC_BUFFER;
  70	}
  71
  72	return vb;
  73}
  74EXPORT_SYMBOL_GPL(videobuf_alloc);
  75
  76#define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
  77				vb->state != VIDEOBUF_QUEUED)
  78int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
  79{
  80	MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
  81
  82	if (non_blocking) {
  83		if (WAITON_CONDITION)
  84			return 0;
  85		else
  86			return -EAGAIN;
  87	}
  88
  89	if (intr)
  90		return wait_event_interruptible(vb->done, WAITON_CONDITION);
  91	else
  92		wait_event(vb->done, WAITON_CONDITION);
  93
  94	return 0;
  95}
  96EXPORT_SYMBOL_GPL(videobuf_waiton);
  97
  98int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
  99		    struct v4l2_framebuffer *fbuf)
 100{
 101	MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
 102	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 103
 104	return CALL(q, iolock, q, vb, fbuf);
 105}
 106EXPORT_SYMBOL_GPL(videobuf_iolock);
 107
 108void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
 109			      struct videobuf_buffer *buf)
 110{
 111	if (q->int_ops->vaddr)
 112		return q->int_ops->vaddr(buf);
 113	return NULL;
 114}
 115EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
 116
 117/* --------------------------------------------------------------------- */
 118
 119
 120void videobuf_queue_core_init(struct videobuf_queue *q,
 121			 const struct videobuf_queue_ops *ops,
 122			 struct device *dev,
 123			 spinlock_t *irqlock,
 124			 enum v4l2_buf_type type,
 125			 enum v4l2_field field,
 126			 unsigned int msize,
 127			 void *priv,
 128			 struct videobuf_qtype_ops *int_ops)
 129{
 130	BUG_ON(!q);
 131	memset(q, 0, sizeof(*q));
 132	q->irqlock   = irqlock;
 133	q->dev       = dev;
 134	q->type      = type;
 135	q->field     = field;
 136	q->msize     = msize;
 137	q->ops       = ops;
 138	q->priv_data = priv;
 139	q->int_ops   = int_ops;
 140
 141	/* All buffer operations are mandatory */
 142	BUG_ON(!q->ops->buf_setup);
 143	BUG_ON(!q->ops->buf_prepare);
 144	BUG_ON(!q->ops->buf_queue);
 145	BUG_ON(!q->ops->buf_release);
 146
 147	/* Lock is mandatory for queue_cancel to work */
 148	BUG_ON(!irqlock);
 149
 150	/* Having implementations for abstract methods are mandatory */
 151	BUG_ON(!q->int_ops);
 152
 153	mutex_init(&q->vb_lock);
 154	init_waitqueue_head(&q->wait);
 155	INIT_LIST_HEAD(&q->stream);
 156}
 157EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
 158
 159/* Locking: Only usage in bttv unsafe find way to remove */
 160int videobuf_queue_is_busy(struct videobuf_queue *q)
 161{
 162	int i;
 163
 164	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 165
 166	if (q->streaming) {
 167		dprintk(1, "busy: streaming active\n");
 168		return 1;
 169	}
 170	if (q->reading) {
 171		dprintk(1, "busy: pending read #1\n");
 172		return 1;
 173	}
 174	if (q->read_buf) {
 175		dprintk(1, "busy: pending read #2\n");
 176		return 1;
 177	}
 178	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 179		if (NULL == q->bufs[i])
 180			continue;
 181		if (q->bufs[i]->map) {
 182			dprintk(1, "busy: buffer #%d mapped\n", i);
 183			return 1;
 184		}
 185		if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
 186			dprintk(1, "busy: buffer #%d queued\n", i);
 187			return 1;
 188		}
 189		if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
 190			dprintk(1, "busy: buffer #%d avtive\n", i);
 191			return 1;
 192		}
 193	}
 194	return 0;
 195}
 196EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
 197
 198/* Locking: Caller holds q->vb_lock */
 199void videobuf_queue_cancel(struct videobuf_queue *q)
 200{
 201	unsigned long flags = 0;
 202	int i;
 203
 204	q->streaming = 0;
 205	q->reading  = 0;
 206	wake_up_interruptible_sync(&q->wait);
 207
 208	/* remove queued buffers from list */
 209	spin_lock_irqsave(q->irqlock, flags);
 210	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 211		if (NULL == q->bufs[i])
 212			continue;
 213		if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
 214			list_del(&q->bufs[i]->queue);
 215			q->bufs[i]->state = VIDEOBUF_ERROR;
 216			wake_up_all(&q->bufs[i]->done);
 217		}
 218	}
 219	spin_unlock_irqrestore(q->irqlock, flags);
 220
 221	/* free all buffers + clear queue */
 222	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 223		if (NULL == q->bufs[i])
 224			continue;
 225		q->ops->buf_release(q, q->bufs[i]);
 226	}
 227	INIT_LIST_HEAD(&q->stream);
 228}
 229EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
 230
 231/* --------------------------------------------------------------------- */
 232
 233/* Locking: Caller holds q->vb_lock */
 234enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
 235{
 236	enum v4l2_field field = q->field;
 237
 238	BUG_ON(V4L2_FIELD_ANY == field);
 239
 240	if (V4L2_FIELD_ALTERNATE == field) {
 241		if (V4L2_FIELD_TOP == q->last) {
 242			field   = V4L2_FIELD_BOTTOM;
 243			q->last = V4L2_FIELD_BOTTOM;
 244		} else {
 245			field   = V4L2_FIELD_TOP;
 246			q->last = V4L2_FIELD_TOP;
 247		}
 248	}
 249	return field;
 250}
 251EXPORT_SYMBOL_GPL(videobuf_next_field);
 252
 253/* Locking: Caller holds q->vb_lock */
 254static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
 255			    struct videobuf_buffer *vb, enum v4l2_buf_type type)
 256{
 257	MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
 258	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 259
 260	b->index    = vb->i;
 261	b->type     = type;
 262
 263	b->memory   = vb->memory;
 264	switch (b->memory) {
 265	case V4L2_MEMORY_MMAP:
 266		b->m.offset  = vb->boff;
 267		b->length    = vb->bsize;
 268		break;
 269	case V4L2_MEMORY_USERPTR:
 270		b->m.userptr = vb->baddr;
 271		b->length    = vb->bsize;
 272		break;
 273	case V4L2_MEMORY_OVERLAY:
 274		b->m.offset  = vb->boff;
 275		break;
 276	}
 277
 278	b->flags    = 0;
 279	if (vb->map)
 280		b->flags |= V4L2_BUF_FLAG_MAPPED;
 281
 282	switch (vb->state) {
 283	case VIDEOBUF_PREPARED:
 284	case VIDEOBUF_QUEUED:
 285	case VIDEOBUF_ACTIVE:
 286		b->flags |= V4L2_BUF_FLAG_QUEUED;
 287		break;
 288	case VIDEOBUF_ERROR:
 289		b->flags |= V4L2_BUF_FLAG_ERROR;
 290		/* fall through */
 291	case VIDEOBUF_DONE:
 292		b->flags |= V4L2_BUF_FLAG_DONE;
 293		break;
 294	case VIDEOBUF_NEEDS_INIT:
 295	case VIDEOBUF_IDLE:
 296		/* nothing */
 297		break;
 298	}
 299
 300	if (vb->input != UNSET) {
 301		b->flags |= V4L2_BUF_FLAG_INPUT;
 302		b->input  = vb->input;
 303	}
 304
 305	b->field     = vb->field;
 306	b->timestamp = vb->ts;
 307	b->bytesused = vb->size;
 308	b->sequence  = vb->field_count >> 1;
 309}
 310
 311/* Locking: Caller holds q->vb_lock */
 312static int __videobuf_mmap_free(struct videobuf_queue *q)
 313{
 314	int i;
 315
 316	if (!q)
 317		return 0;
 318
 319	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 320
 321	for (i = 0; i < VIDEO_MAX_FRAME; i++)
 322		if (q->bufs[i] && q->bufs[i]->map)
 323			return -EBUSY;
 324
 325	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 326		if (NULL == q->bufs[i])
 327			continue;
 328		q->ops->buf_release(q, q->bufs[i]);
 329		kfree(q->bufs[i]);
 330		q->bufs[i] = NULL;
 331	}
 332
 333	return 0;
 334}
 335
 336int videobuf_mmap_free(struct videobuf_queue *q)
 337{
 338	int ret;
 339	mutex_lock(&q->vb_lock);
 340	ret = __videobuf_mmap_free(q);
 341	mutex_unlock(&q->vb_lock);
 342	return ret;
 343}
 344EXPORT_SYMBOL_GPL(videobuf_mmap_free);
 345
 346/* Locking: Caller holds q->vb_lock */
 347int __videobuf_mmap_setup(struct videobuf_queue *q,
 348			unsigned int bcount, unsigned int bsize,
 349			enum v4l2_memory memory)
 350{
 351	unsigned int i;
 352	int err;
 353
 354	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 355
 356	err = __videobuf_mmap_free(q);
 357	if (0 != err)
 358		return err;
 359
 360	/* Allocate and initialize buffers */
 361	for (i = 0; i < bcount; i++) {
 362		q->bufs[i] = videobuf_alloc(q);
 363
 364		if (NULL == q->bufs[i])
 365			break;
 366
 367		q->bufs[i]->i      = i;
 368		q->bufs[i]->input  = UNSET;
 369		q->bufs[i]->memory = memory;
 370		q->bufs[i]->bsize  = bsize;
 371		switch (memory) {
 372		case V4L2_MEMORY_MMAP:
 373			q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
 374			break;
 375		case V4L2_MEMORY_USERPTR:
 376		case V4L2_MEMORY_OVERLAY:
 377			/* nothing */
 378			break;
 379		}
 380	}
 381
 382	if (!i)
 383		return -ENOMEM;
 384
 385	dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
 386
 387	return i;
 388}
 389EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
 390
 391int videobuf_mmap_setup(struct videobuf_queue *q,
 392			unsigned int bcount, unsigned int bsize,
 393			enum v4l2_memory memory)
 394{
 395	int ret;
 396	mutex_lock(&q->vb_lock);
 397	ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
 398	mutex_unlock(&q->vb_lock);
 399	return ret;
 400}
 401EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
 402
 403int videobuf_reqbufs(struct videobuf_queue *q,
 404		 struct v4l2_requestbuffers *req)
 405{
 406	unsigned int size, count;
 407	int retval;
 408
 409	if (req->count < 1) {
 410		dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
 411		return -EINVAL;
 412	}
 413
 414	if (req->memory != V4L2_MEMORY_MMAP     &&
 415	    req->memory != V4L2_MEMORY_USERPTR  &&
 416	    req->memory != V4L2_MEMORY_OVERLAY) {
 417		dprintk(1, "reqbufs: memory type invalid\n");
 418		return -EINVAL;
 419	}
 420
 421	mutex_lock(&q->vb_lock);
 422	if (req->type != q->type) {
 423		dprintk(1, "reqbufs: queue type invalid\n");
 424		retval = -EINVAL;
 425		goto done;
 426	}
 427
 428	if (q->streaming) {
 429		dprintk(1, "reqbufs: streaming already exists\n");
 430		retval = -EBUSY;
 431		goto done;
 432	}
 433	if (!list_empty(&q->stream)) {
 434		dprintk(1, "reqbufs: stream running\n");
 435		retval = -EBUSY;
 436		goto done;
 437	}
 438
 439	count = req->count;
 440	if (count > VIDEO_MAX_FRAME)
 441		count = VIDEO_MAX_FRAME;
 442	size = 0;
 443	q->ops->buf_setup(q, &count, &size);
 444	dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
 445		count, size,
 446		(unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
 447
 448	retval = __videobuf_mmap_setup(q, count, size, req->memory);
 449	if (retval < 0) {
 450		dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
 451		goto done;
 452	}
 453
 454	req->count = retval;
 455	retval = 0;
 456
 457 done:
 458	mutex_unlock(&q->vb_lock);
 459	return retval;
 460}
 461EXPORT_SYMBOL_GPL(videobuf_reqbufs);
 462
 463int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
 464{
 465	int ret = -EINVAL;
 466
 467	mutex_lock(&q->vb_lock);
 468	if (unlikely(b->type != q->type)) {
 469		dprintk(1, "querybuf: Wrong type.\n");
 470		goto done;
 471	}
 472	if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
 473		dprintk(1, "querybuf: index out of range.\n");
 474		goto done;
 475	}
 476	if (unlikely(NULL == q->bufs[b->index])) {
 477		dprintk(1, "querybuf: buffer is null.\n");
 478		goto done;
 479	}
 480
 481	videobuf_status(q, b, q->bufs[b->index], q->type);
 482
 483	ret = 0;
 484done:
 485	mutex_unlock(&q->vb_lock);
 486	return ret;
 487}
 488EXPORT_SYMBOL_GPL(videobuf_querybuf);
 489
 490int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
 491{
 492	struct videobuf_buffer *buf;
 493	enum v4l2_field field;
 494	unsigned long flags = 0;
 495	int retval;
 496
 497	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 498
 499	if (b->memory == V4L2_MEMORY_MMAP)
 500		down_read(&current->mm->mmap_sem);
 501
 502	mutex_lock(&q->vb_lock);
 503	retval = -EBUSY;
 504	if (q->reading) {
 505		dprintk(1, "qbuf: Reading running...\n");
 506		goto done;
 507	}
 508	retval = -EINVAL;
 509	if (b->type != q->type) {
 510		dprintk(1, "qbuf: Wrong type.\n");
 511		goto done;
 512	}
 513	if (b->index >= VIDEO_MAX_FRAME) {
 514		dprintk(1, "qbuf: index out of range.\n");
 515		goto done;
 516	}
 517	buf = q->bufs[b->index];
 518	if (NULL == buf) {
 519		dprintk(1, "qbuf: buffer is null.\n");
 520		goto done;
 521	}
 522	MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
 523	if (buf->memory != b->memory) {
 524		dprintk(1, "qbuf: memory type is wrong.\n");
 525		goto done;
 526	}
 527	if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
 528		dprintk(1, "qbuf: buffer is already queued or active.\n");
 529		goto done;
 530	}
 531
 532	if (b->flags & V4L2_BUF_FLAG_INPUT) {
 533		if (b->input >= q->inputs) {
 534			dprintk(1, "qbuf: wrong input.\n");
 535			goto done;
 536		}
 537		buf->input = b->input;
 538	} else {
 539		buf->input = UNSET;
 540	}
 541
 542	switch (b->memory) {
 543	case V4L2_MEMORY_MMAP:
 544		if (0 == buf->baddr) {
 545			dprintk(1, "qbuf: mmap requested "
 546				   "but buffer addr is zero!\n");
 547			goto done;
 548		}
 549		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
 550		    || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
 551		    || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
 552			buf->size = b->bytesused;
 553			buf->field = b->field;
 554			buf->ts = b->timestamp;
 555		}
 556		break;
 557	case V4L2_MEMORY_USERPTR:
 558		if (b->length < buf->bsize) {
 559			dprintk(1, "qbuf: buffer length is not enough\n");
 560			goto done;
 561		}
 562		if (VIDEOBUF_NEEDS_INIT != buf->state &&
 563		    buf->baddr != b->m.userptr)
 564			q->ops->buf_release(q, buf);
 565		buf->baddr = b->m.userptr;
 566		break;
 567	case V4L2_MEMORY_OVERLAY:
 568		buf->boff = b->m.offset;
 569		break;
 570	default:
 571		dprintk(1, "qbuf: wrong memory type\n");
 572		goto done;
 573	}
 574
 575	dprintk(1, "qbuf: requesting next field\n");
 576	field = videobuf_next_field(q);
 577	retval = q->ops->buf_prepare(q, buf, field);
 578	if (0 != retval) {
 579		dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
 580		goto done;
 581	}
 582
 583	list_add_tail(&buf->stream, &q->stream);
 584	if (q->streaming) {
 585		spin_lock_irqsave(q->irqlock, flags);
 586		q->ops->buf_queue(q, buf);
 587		spin_unlock_irqrestore(q->irqlock, flags);
 588	}
 589	dprintk(1, "qbuf: succeeded\n");
 590	retval = 0;
 591	wake_up_interruptible_sync(&q->wait);
 592
 593done:
 594	mutex_unlock(&q->vb_lock);
 595
 596	if (b->memory == V4L2_MEMORY_MMAP)
 597		up_read(&current->mm->mmap_sem);
 598
 599	return retval;
 600}
 601EXPORT_SYMBOL_GPL(videobuf_qbuf);
 602
 603/* Locking: Caller holds q->vb_lock */
 604static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
 605{
 606	int retval;
 607
 608checks:
 609	if (!q->streaming) {
 610		dprintk(1, "next_buffer: Not streaming\n");
 611		retval = -EINVAL;
 612		goto done;
 613	}
 614
 615	if (list_empty(&q->stream)) {
 616		if (noblock) {
 617			retval = -EAGAIN;
 618			dprintk(2, "next_buffer: no buffers to dequeue\n");
 619			goto done;
 620		} else {
 621			dprintk(2, "next_buffer: waiting on buffer\n");
 622
 623			/* Drop lock to avoid deadlock with qbuf */
 624			mutex_unlock(&q->vb_lock);
 625
 626			/* Checking list_empty and streaming is safe without
 627			 * locks because we goto checks to validate while
 628			 * holding locks before proceeding */
 629			retval = wait_event_interruptible(q->wait,
 630				!list_empty(&q->stream) || !q->streaming);
 631			mutex_lock(&q->vb_lock);
 632
 633			if (retval)
 634				goto done;
 635
 636			goto checks;
 637		}
 638	}
 639
 640	retval = 0;
 641
 642done:
 643	return retval;
 644}
 645
 646/* Locking: Caller holds q->vb_lock */
 647static int stream_next_buffer(struct videobuf_queue *q,
 648			struct videobuf_buffer **vb, int nonblocking)
 649{
 650	int retval;
 651	struct videobuf_buffer *buf = NULL;
 652
 653	retval = stream_next_buffer_check_queue(q, nonblocking);
 654	if (retval)
 655		goto done;
 656
 657	buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
 658	retval = videobuf_waiton(buf, nonblocking, 1);
 659	if (retval < 0)
 660		goto done;
 661
 662	*vb = buf;
 663done:
 664	return retval;
 665}
 666
 667int videobuf_dqbuf(struct videobuf_queue *q,
 668		   struct v4l2_buffer *b, int nonblocking)
 669{
 670	struct videobuf_buffer *buf = NULL;
 671	int retval;
 672
 673	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 674
 675	memset(b, 0, sizeof(*b));
 676	mutex_lock(&q->vb_lock);
 677
 678	retval = stream_next_buffer(q, &buf, nonblocking);
 679	if (retval < 0) {
 680		dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
 681		goto done;
 682	}
 683
 684	switch (buf->state) {
 685	case VIDEOBUF_ERROR:
 686		dprintk(1, "dqbuf: state is error\n");
 687		break;
 688	case VIDEOBUF_DONE:
 689		dprintk(1, "dqbuf: state is done\n");
 690		break;
 691	default:
 692		dprintk(1, "dqbuf: state invalid\n");
 693		retval = -EINVAL;
 694		goto done;
 695	}
 696	CALL(q, sync, q, buf);
 697	videobuf_status(q, b, buf, q->type);
 698	list_del(&buf->stream);
 699	buf->state = VIDEOBUF_IDLE;
 700	b->flags &= ~V4L2_BUF_FLAG_DONE;
 701done:
 702	mutex_unlock(&q->vb_lock);
 703	return retval;
 704}
 705EXPORT_SYMBOL_GPL(videobuf_dqbuf);
 706
 707int videobuf_streamon(struct videobuf_queue *q)
 708{
 709	struct videobuf_buffer *buf;
 710	unsigned long flags = 0;
 711	int retval;
 712
 713	mutex_lock(&q->vb_lock);
 714	retval = -EBUSY;
 715	if (q->reading)
 716		goto done;
 717	retval = 0;
 718	if (q->streaming)
 719		goto done;
 720	q->streaming = 1;
 721	spin_lock_irqsave(q->irqlock, flags);
 722	list_for_each_entry(buf, &q->stream, stream)
 723		if (buf->state == VIDEOBUF_PREPARED)
 724			q->ops->buf_queue(q, buf);
 725	spin_unlock_irqrestore(q->irqlock, flags);
 726
 727	wake_up_interruptible_sync(&q->wait);
 728done:
 729	mutex_unlock(&q->vb_lock);
 730	return retval;
 731}
 732EXPORT_SYMBOL_GPL(videobuf_streamon);
 733
 734/* Locking: Caller holds q->vb_lock */
 735static int __videobuf_streamoff(struct videobuf_queue *q)
 736{
 737	if (!q->streaming)
 738		return -EINVAL;
 739
 740	videobuf_queue_cancel(q);
 741
 742	return 0;
 743}
 744
 745int videobuf_streamoff(struct videobuf_queue *q)
 746{
 747	int retval;
 748
 749	mutex_lock(&q->vb_lock);
 750	retval = __videobuf_streamoff(q);
 751	mutex_unlock(&q->vb_lock);
 752
 753	return retval;
 754}
 755EXPORT_SYMBOL_GPL(videobuf_streamoff);
 756
 757/* Locking: Caller holds q->vb_lock */
 758static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
 759				      char __user *data,
 760				      size_t count, loff_t *ppos)
 761{
 762	enum v4l2_field field;
 763	unsigned long flags = 0;
 764	int retval;
 765
 766	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 767
 768	/* setup stuff */
 769	q->read_buf = videobuf_alloc(q);
 770	if (NULL == q->read_buf)
 771		return -ENOMEM;
 772
 773	q->read_buf->memory = V4L2_MEMORY_USERPTR;
 774	q->read_buf->baddr  = (unsigned long)data;
 775	q->read_buf->bsize  = count;
 776
 777	field = videobuf_next_field(q);
 778	retval = q->ops->buf_prepare(q, q->read_buf, field);
 779	if (0 != retval)
 780		goto done;
 781
 782	/* start capture & wait */
 783	spin_lock_irqsave(q->irqlock, flags);
 784	q->ops->buf_queue(q, q->read_buf);
 785	spin_unlock_irqrestore(q->irqlock, flags);
 786	retval = videobuf_waiton(q->read_buf, 0, 0);
 787	if (0 == retval) {
 788		CALL(q, sync, q, q->read_buf);
 789		if (VIDEOBUF_ERROR == q->read_buf->state)
 790			retval = -EIO;
 791		else
 792			retval = q->read_buf->size;
 793	}
 794
 795done:
 796	/* cleanup */
 797	q->ops->buf_release(q, q->read_buf);
 798	kfree(q->read_buf);
 799	q->read_buf = NULL;
 800	return retval;
 801}
 802
 803static int __videobuf_copy_to_user(struct videobuf_queue *q,
 804				   struct videobuf_buffer *buf,
 805				   char __user *data, size_t count,
 806				   int nonblocking)
 807{
 808	void *vaddr = CALL(q, vaddr, buf);
 809
 810	/* copy to userspace */
 811	if (count > buf->size - q->read_off)
 812		count = buf->size - q->read_off;
 813
 814	if (copy_to_user(data, vaddr + q->read_off, count))
 815		return -EFAULT;
 816
 817	return count;
 818}
 819
 820static int __videobuf_copy_stream(struct videobuf_queue *q,
 821				  struct videobuf_buffer *buf,
 822				  char __user *data, size_t count, size_t pos,
 823				  int vbihack, int nonblocking)
 824{
 825	unsigned int *fc = CALL(q, vaddr, buf);
 826
 827	if (vbihack) {
 828		/* dirty, undocumented hack -- pass the frame counter
 829			* within the last four bytes of each vbi data block.
 830			* We need that one to maintain backward compatibility
 831			* to all vbi decoding software out there ... */
 832		fc += (buf->size >> 2) - 1;
 833		*fc = buf->field_count >> 1;
 834		dprintk(1, "vbihack: %d\n", *fc);
 835	}
 836
 837	/* copy stuff using the common method */
 838	count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
 839
 840	if ((count == -EFAULT) && (pos == 0))
 841		return -EFAULT;
 842
 843	return count;
 844}
 845
 846ssize_t videobuf_read_one(struct videobuf_queue *q,
 847			  char __user *data, size_t count, loff_t *ppos,
 848			  int nonblocking)
 849{
 850	enum v4l2_field field;
 851	unsigned long flags = 0;
 852	unsigned size = 0, nbufs = 1;
 853	int retval;
 854
 855	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
 856
 857	mutex_lock(&q->vb_lock);
 858
 859	q->ops->buf_setup(q, &nbufs, &size);
 860
 861	if (NULL == q->read_buf  &&
 862	    count >= size        &&
 863	    !nonblocking) {
 864		retval = videobuf_read_zerocopy(q, data, count, ppos);
 865		if (retval >= 0  ||  retval == -EIO)
 866			/* ok, all done */
 867			goto done;
 868		/* fallback to kernel bounce buffer on failures */
 869	}
 870
 871	if (NULL == q->read_buf) {
 872		/* need to capture a new frame */
 873		retval = -ENOMEM;
 874		q->read_buf = videobuf_alloc(q);
 875
 876		dprintk(1, "video alloc=0x%p\n", q->read_buf);
 877		if (NULL == q->read_buf)
 878			goto done;
 879		q->read_buf->memory = V4L2_MEMORY_USERPTR;
 880		q->read_buf->bsize = count; /* preferred size */
 881		field = videobuf_next_field(q);
 882		retval = q->ops->buf_prepare(q, q->read_buf, field);
 883
 884		if (0 != retval) {
 885			kfree(q->read_buf);
 886			q->read_buf = NULL;
 887			goto done;
 888		}
 889
 890		spin_lock_irqsave(q->irqlock, flags);
 891		q->ops->buf_queue(q, q->read_buf);
 892		spin_unlock_irqrestore(q->irqlock, flags);
 893
 894		q->read_off = 0;
 895	}
 896
 897	/* wait until capture is done */
 898	retval = videobuf_waiton(q->read_buf, nonblocking, 1);
 899	if (0 != retval)
 900		goto done;
 901
 902	CALL(q, sync, q, q->read_buf);
 903
 904	if (VIDEOBUF_ERROR == q->read_buf->state) {
 905		/* catch I/O errors */
 906		q->ops->buf_release(q, q->read_buf);
 907		kfree(q->read_buf);
 908		q->read_buf = NULL;
 909		retval = -EIO;
 910		goto done;
 911	}
 912
 913	/* Copy to userspace */
 914	retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
 915	if (retval < 0)
 916		goto done;
 917
 918	q->read_off += retval;
 919	if (q->read_off == q->read_buf->size) {
 920		/* all data copied, cleanup */
 921		q->ops->buf_release(q, q->read_buf);
 922		kfree(q->read_buf);
 923		q->read_buf = NULL;
 924	}
 925
 926done:
 927	mutex_unlock(&q->vb_lock);
 928	return retval;
 929}
 930EXPORT_SYMBOL_GPL(videobuf_read_one);
 931
 932/* Locking: Caller holds q->vb_lock */
 933static int __videobuf_read_start(struct videobuf_queue *q)
 934{
 935	enum v4l2_field field;
 936	unsigned long flags = 0;
 937	unsigned int count = 0, size = 0;
 938	int err, i;
 939
 940	q->ops->buf_setup(q, &count, &size);
 941	if (count < 2)
 942		count = 2;
 943	if (count > VIDEO_MAX_FRAME)
 944		count = VIDEO_MAX_FRAME;
 945	size = PAGE_ALIGN(size);
 946
 947	err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
 948	if (err < 0)
 949		return err;
 950
 951	count = err;
 952
 953	for (i = 0; i < count; i++) {
 954		field = videobuf_next_field(q);
 955		err = q->ops->buf_prepare(q, q->bufs[i], field);
 956		if (err)
 957			return err;
 958		list_add_tail(&q->bufs[i]->stream, &q->stream);
 959	}
 960	spin_lock_irqsave(q->irqlock, flags);
 961	for (i = 0; i < count; i++)
 962		q->ops->buf_queue(q, q->bufs[i]);
 963	spin_unlock_irqrestore(q->irqlock, flags);
 964	q->reading = 1;
 965	return 0;
 966}
 967
 968static void __videobuf_read_stop(struct videobuf_queue *q)
 969{
 970	int i;
 971
 972	videobuf_queue_cancel(q);
 973	__videobuf_mmap_free(q);
 974	INIT_LIST_HEAD(&q->stream);
 975	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
 976		if (NULL == q->bufs[i])
 977			continue;
 978		kfree(q->bufs[i]);
 979		q->bufs[i] = NULL;
 980	}
 981	q->read_buf = NULL;
 982}
 983
 984int videobuf_read_start(struct videobuf_queue *q)
 985{
 986	int rc;
 987
 988	mutex_lock(&q->vb_lock);
 989	rc = __videobuf_read_start(q);
 990	mutex_unlock(&q->vb_lock);
 991
 992	return rc;
 993}
 994EXPORT_SYMBOL_GPL(videobuf_read_start);
 995
 996void videobuf_read_stop(struct videobuf_queue *q)
 997{
 998	mutex_lock(&q->vb_lock);
 999	__videobuf_read_stop(q);
1000	mutex_unlock(&q->vb_lock);
1001}
1002EXPORT_SYMBOL_GPL(videobuf_read_stop);
1003
1004void videobuf_stop(struct videobuf_queue *q)
1005{
1006	mutex_lock(&q->vb_lock);
1007
1008	if (q->streaming)
1009		__videobuf_streamoff(q);
1010
1011	if (q->reading)
1012		__videobuf_read_stop(q);
1013
1014	mutex_unlock(&q->vb_lock);
1015}
1016EXPORT_SYMBOL_GPL(videobuf_stop);
1017
1018ssize_t videobuf_read_stream(struct videobuf_queue *q,
1019			     char __user *data, size_t count, loff_t *ppos,
1020			     int vbihack, int nonblocking)
1021{
1022	int rc, retval;
1023	unsigned long flags = 0;
1024
1025	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1026
1027	dprintk(2, "%s\n", __func__);
1028	mutex_lock(&q->vb_lock);
1029	retval = -EBUSY;
1030	if (q->streaming)
1031		goto done;
1032	if (!q->reading) {
1033		retval = __videobuf_read_start(q);
1034		if (retval < 0)
1035			goto done;
1036	}
1037
1038	retval = 0;
1039	while (count > 0) {
1040		/* get / wait for data */
1041		if (NULL == q->read_buf) {
1042			q->read_buf = list_entry(q->stream.next,
1043						 struct videobuf_buffer,
1044						 stream);
1045			list_del(&q->read_buf->stream);
1046			q->read_off = 0;
1047		}
1048		rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1049		if (rc < 0) {
1050			if (0 == retval)
1051				retval = rc;
1052			break;
1053		}
1054
1055		if (q->read_buf->state == VIDEOBUF_DONE) {
1056			rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
1057					retval, vbihack, nonblocking);
1058			if (rc < 0) {
1059				retval = rc;
1060				break;
1061			}
1062			retval      += rc;
1063			count       -= rc;
1064			q->read_off += rc;
1065		} else {
1066			/* some error */
1067			q->read_off = q->read_buf->size;
1068			if (0 == retval)
1069				retval = -EIO;
1070		}
1071
1072		/* requeue buffer when done with copying */
1073		if (q->read_off == q->read_buf->size) {
1074			list_add_tail(&q->read_buf->stream,
1075				      &q->stream);
1076			spin_lock_irqsave(q->irqlock, flags);
1077			q->ops->buf_queue(q, q->read_buf);
1078			spin_unlock_irqrestore(q->irqlock, flags);
1079			q->read_buf = NULL;
1080		}
1081		if (retval < 0)
1082			break;
1083	}
1084
1085done:
1086	mutex_unlock(&q->vb_lock);
1087	return retval;
1088}
1089EXPORT_SYMBOL_GPL(videobuf_read_stream);
1090
1091unsigned int videobuf_poll_stream(struct file *file,
1092				  struct videobuf_queue *q,
1093				  poll_table *wait)
1094{
1095	struct videobuf_buffer *buf = NULL;
1096	unsigned int rc = 0;
1097
1098	mutex_lock(&q->vb_lock);
1099	if (q->streaming) {
1100		if (!list_empty(&q->stream))
1101			buf = list_entry(q->stream.next,
1102					 struct videobuf_buffer, stream);
1103	} else {
1104		if (!q->reading)
1105			__videobuf_read_start(q);
1106		if (!q->reading) {
1107			rc = POLLERR;
1108		} else if (NULL == q->read_buf) {
1109			q->read_buf = list_entry(q->stream.next,
1110						 struct videobuf_buffer,
1111						 stream);
1112			list_del(&q->read_buf->stream);
1113			q->read_off = 0;
1114		}
1115		buf = q->read_buf;
1116	}
1117	if (!buf)
1118		rc = POLLERR;
1119
1120	if (0 == rc) {
1121		poll_wait(file, &buf->done, wait);
1122		if (buf->state == VIDEOBUF_DONE ||
1123		    buf->state == VIDEOBUF_ERROR) {
1124			switch (q->type) {
1125			case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1126			case V4L2_BUF_TYPE_VBI_OUTPUT:
1127			case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1128				rc = POLLOUT | POLLWRNORM;
1129				break;
1130			default:
1131				rc = POLLIN | POLLRDNORM;
1132				break;
1133			}
1134		}
1135	}
1136	mutex_unlock(&q->vb_lock);
1137	return rc;
1138}
1139EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1140
1141int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1142{
1143	int rc = -EINVAL;
1144	int i;
1145
1146	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1147
1148	if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1149		dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1150		return -EINVAL;
1151	}
1152
1153	mutex_lock(&q->vb_lock);
1154	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1155		struct videobuf_buffer *buf = q->bufs[i];
1156
1157		if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1158				buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1159			rc = CALL(q, mmap_mapper, q, buf, vma);
1160			break;
1161		}
1162	}
1163	mutex_unlock(&q->vb_lock);
1164
1165	return rc;
1166}
1167EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1168
1169#ifdef CONFIG_VIDEO_V4L1_COMPAT
1170int videobuf_cgmbuf(struct videobuf_queue *q,
1171		    struct video_mbuf *mbuf, int count)
1172{
1173	struct v4l2_requestbuffers req;
1174	int rc, i;
1175
1176	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1177
1178	memset(&req, 0, sizeof(req));
1179	req.type   = q->type;
1180	req.count  = count;
1181	req.memory = V4L2_MEMORY_MMAP;
1182	rc = videobuf_reqbufs(q, &req);
1183	if (rc < 0)
1184		return rc;
1185
1186	mbuf->frames = req.count;
1187	mbuf->size   = 0;
1188	for (i = 0; i < mbuf->frames; i++) {
1189		mbuf->offsets[i]  = q->bufs[i]->boff;
1190		mbuf->size       += PAGE_ALIGN(q->bufs[i]->bsize);
1191	}
1192
1193	return 0;
1194}
1195EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1196#endif
1197