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