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    = 0;
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->count < 1) {
445 		dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
446 		return -EINVAL;
447 	}
448 
449 	if (req->memory != V4L2_MEMORY_MMAP     &&
450 	    req->memory != V4L2_MEMORY_USERPTR  &&
451 	    req->memory != V4L2_MEMORY_OVERLAY) {
452 		dprintk(1, "reqbufs: memory type invalid\n");
453 		return -EINVAL;
454 	}
455 
456 	videobuf_queue_lock(q);
457 	if (req->type != q->type) {
458 		dprintk(1, "reqbufs: queue type invalid\n");
459 		retval = -EINVAL;
460 		goto done;
461 	}
462 
463 	if (q->streaming) {
464 		dprintk(1, "reqbufs: streaming already exists\n");
465 		retval = -EBUSY;
466 		goto done;
467 	}
468 	if (!list_empty(&q->stream)) {
469 		dprintk(1, "reqbufs: stream running\n");
470 		retval = -EBUSY;
471 		goto done;
472 	}
473 
474 	count = req->count;
475 	if (count > VIDEO_MAX_FRAME)
476 		count = VIDEO_MAX_FRAME;
477 	size = 0;
478 	q->ops->buf_setup(q, &count, &size);
479 	dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
480 		count, size,
481 		(unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
482 
483 	retval = __videobuf_mmap_setup(q, count, size, req->memory);
484 	if (retval < 0) {
485 		dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
486 		goto done;
487 	}
488 
489 	req->count = retval;
490 	retval = 0;
491 
492  done:
493 	videobuf_queue_unlock(q);
494 	return retval;
495 }
496 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
497 
498 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
499 {
500 	int ret = -EINVAL;
501 
502 	videobuf_queue_lock(q);
503 	if (unlikely(b->type != q->type)) {
504 		dprintk(1, "querybuf: Wrong type.\n");
505 		goto done;
506 	}
507 	if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
508 		dprintk(1, "querybuf: index out of range.\n");
509 		goto done;
510 	}
511 	if (unlikely(NULL == q->bufs[b->index])) {
512 		dprintk(1, "querybuf: buffer is null.\n");
513 		goto done;
514 	}
515 
516 	videobuf_status(q, b, q->bufs[b->index], q->type);
517 
518 	ret = 0;
519 done:
520 	videobuf_queue_unlock(q);
521 	return ret;
522 }
523 EXPORT_SYMBOL_GPL(videobuf_querybuf);
524 
525 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
526 {
527 	struct videobuf_buffer *buf;
528 	enum v4l2_field field;
529 	unsigned long flags = 0;
530 	int retval;
531 
532 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
533 
534 	if (b->memory == V4L2_MEMORY_MMAP)
535 		down_read(&current->mm->mmap_sem);
536 
537 	videobuf_queue_lock(q);
538 	retval = -EBUSY;
539 	if (q->reading) {
540 		dprintk(1, "qbuf: Reading running...\n");
541 		goto done;
542 	}
543 	retval = -EINVAL;
544 	if (b->type != q->type) {
545 		dprintk(1, "qbuf: Wrong type.\n");
546 		goto done;
547 	}
548 	if (b->index >= VIDEO_MAX_FRAME) {
549 		dprintk(1, "qbuf: index out of range.\n");
550 		goto done;
551 	}
552 	buf = q->bufs[b->index];
553 	if (NULL == buf) {
554 		dprintk(1, "qbuf: buffer is null.\n");
555 		goto done;
556 	}
557 	MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
558 	if (buf->memory != b->memory) {
559 		dprintk(1, "qbuf: memory type is wrong.\n");
560 		goto done;
561 	}
562 	if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
563 		dprintk(1, "qbuf: buffer is already queued or active.\n");
564 		goto done;
565 	}
566 
567 	switch (b->memory) {
568 	case V4L2_MEMORY_MMAP:
569 		if (0 == buf->baddr) {
570 			dprintk(1, "qbuf: mmap requested "
571 				   "but buffer addr is zero!\n");
572 			goto done;
573 		}
574 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
575 		    || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
576 		    || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
577 			buf->size = b->bytesused;
578 			buf->field = b->field;
579 			buf->ts = b->timestamp;
580 		}
581 		break;
582 	case V4L2_MEMORY_USERPTR:
583 		if (b->length < buf->bsize) {
584 			dprintk(1, "qbuf: buffer length is not enough\n");
585 			goto done;
586 		}
587 		if (VIDEOBUF_NEEDS_INIT != buf->state &&
588 		    buf->baddr != b->m.userptr)
589 			q->ops->buf_release(q, buf);
590 		buf->baddr = b->m.userptr;
591 		break;
592 	case V4L2_MEMORY_OVERLAY:
593 		buf->boff = b->m.offset;
594 		break;
595 	default:
596 		dprintk(1, "qbuf: wrong memory type\n");
597 		goto done;
598 	}
599 
600 	dprintk(1, "qbuf: requesting next field\n");
601 	field = videobuf_next_field(q);
602 	retval = q->ops->buf_prepare(q, buf, field);
603 	if (0 != retval) {
604 		dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
605 		goto done;
606 	}
607 
608 	list_add_tail(&buf->stream, &q->stream);
609 	if (q->streaming) {
610 		spin_lock_irqsave(q->irqlock, flags);
611 		q->ops->buf_queue(q, buf);
612 		spin_unlock_irqrestore(q->irqlock, flags);
613 	}
614 	dprintk(1, "qbuf: succeeded\n");
615 	retval = 0;
616 	wake_up_interruptible_sync(&q->wait);
617 
618 done:
619 	videobuf_queue_unlock(q);
620 
621 	if (b->memory == V4L2_MEMORY_MMAP)
622 		up_read(&current->mm->mmap_sem);
623 
624 	return retval;
625 }
626 EXPORT_SYMBOL_GPL(videobuf_qbuf);
627 
628 /* Locking: Caller holds q->vb_lock */
629 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
630 {
631 	int retval;
632 
633 checks:
634 	if (!q->streaming) {
635 		dprintk(1, "next_buffer: Not streaming\n");
636 		retval = -EINVAL;
637 		goto done;
638 	}
639 
640 	if (list_empty(&q->stream)) {
641 		if (noblock) {
642 			retval = -EAGAIN;
643 			dprintk(2, "next_buffer: no buffers to dequeue\n");
644 			goto done;
645 		} else {
646 			dprintk(2, "next_buffer: waiting on buffer\n");
647 
648 			/* Drop lock to avoid deadlock with qbuf */
649 			videobuf_queue_unlock(q);
650 
651 			/* Checking list_empty and streaming is safe without
652 			 * locks because we goto checks to validate while
653 			 * holding locks before proceeding */
654 			retval = wait_event_interruptible(q->wait,
655 				!list_empty(&q->stream) || !q->streaming);
656 			videobuf_queue_lock(q);
657 
658 			if (retval)
659 				goto done;
660 
661 			goto checks;
662 		}
663 	}
664 
665 	retval = 0;
666 
667 done:
668 	return retval;
669 }
670 
671 /* Locking: Caller holds q->vb_lock */
672 static int stream_next_buffer(struct videobuf_queue *q,
673 			struct videobuf_buffer **vb, int nonblocking)
674 {
675 	int retval;
676 	struct videobuf_buffer *buf = NULL;
677 
678 	retval = stream_next_buffer_check_queue(q, nonblocking);
679 	if (retval)
680 		goto done;
681 
682 	buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
683 	retval = videobuf_waiton(q, buf, nonblocking, 1);
684 	if (retval < 0)
685 		goto done;
686 
687 	*vb = buf;
688 done:
689 	return retval;
690 }
691 
692 int videobuf_dqbuf(struct videobuf_queue *q,
693 		   struct v4l2_buffer *b, int nonblocking)
694 {
695 	struct videobuf_buffer *buf = NULL;
696 	int retval;
697 
698 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
699 
700 	memset(b, 0, sizeof(*b));
701 	videobuf_queue_lock(q);
702 
703 	retval = stream_next_buffer(q, &buf, nonblocking);
704 	if (retval < 0) {
705 		dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
706 		goto done;
707 	}
708 
709 	switch (buf->state) {
710 	case VIDEOBUF_ERROR:
711 		dprintk(1, "dqbuf: state is error\n");
712 		break;
713 	case VIDEOBUF_DONE:
714 		dprintk(1, "dqbuf: state is done\n");
715 		break;
716 	default:
717 		dprintk(1, "dqbuf: state invalid\n");
718 		retval = -EINVAL;
719 		goto done;
720 	}
721 	CALL(q, sync, q, buf);
722 	videobuf_status(q, b, buf, q->type);
723 	list_del(&buf->stream);
724 	buf->state = VIDEOBUF_IDLE;
725 	b->flags &= ~V4L2_BUF_FLAG_DONE;
726 done:
727 	videobuf_queue_unlock(q);
728 	return retval;
729 }
730 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
731 
732 int videobuf_streamon(struct videobuf_queue *q)
733 {
734 	struct videobuf_buffer *buf;
735 	unsigned long flags = 0;
736 	int retval;
737 
738 	videobuf_queue_lock(q);
739 	retval = -EBUSY;
740 	if (q->reading)
741 		goto done;
742 	retval = 0;
743 	if (q->streaming)
744 		goto done;
745 	q->streaming = 1;
746 	spin_lock_irqsave(q->irqlock, flags);
747 	list_for_each_entry(buf, &q->stream, stream)
748 		if (buf->state == VIDEOBUF_PREPARED)
749 			q->ops->buf_queue(q, buf);
750 	spin_unlock_irqrestore(q->irqlock, flags);
751 
752 	wake_up_interruptible_sync(&q->wait);
753 done:
754 	videobuf_queue_unlock(q);
755 	return retval;
756 }
757 EXPORT_SYMBOL_GPL(videobuf_streamon);
758 
759 /* Locking: Caller holds q->vb_lock */
760 static int __videobuf_streamoff(struct videobuf_queue *q)
761 {
762 	if (!q->streaming)
763 		return -EINVAL;
764 
765 	videobuf_queue_cancel(q);
766 
767 	return 0;
768 }
769 
770 int videobuf_streamoff(struct videobuf_queue *q)
771 {
772 	int retval;
773 
774 	videobuf_queue_lock(q);
775 	retval = __videobuf_streamoff(q);
776 	videobuf_queue_unlock(q);
777 
778 	return retval;
779 }
780 EXPORT_SYMBOL_GPL(videobuf_streamoff);
781 
782 /* Locking: Caller holds q->vb_lock */
783 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
784 				      char __user *data,
785 				      size_t count, loff_t *ppos)
786 {
787 	enum v4l2_field field;
788 	unsigned long flags = 0;
789 	int retval;
790 
791 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
792 
793 	/* setup stuff */
794 	q->read_buf = videobuf_alloc_vb(q);
795 	if (NULL == q->read_buf)
796 		return -ENOMEM;
797 
798 	q->read_buf->memory = V4L2_MEMORY_USERPTR;
799 	q->read_buf->baddr  = (unsigned long)data;
800 	q->read_buf->bsize  = count;
801 
802 	field = videobuf_next_field(q);
803 	retval = q->ops->buf_prepare(q, q->read_buf, field);
804 	if (0 != retval)
805 		goto done;
806 
807 	/* start capture & wait */
808 	spin_lock_irqsave(q->irqlock, flags);
809 	q->ops->buf_queue(q, q->read_buf);
810 	spin_unlock_irqrestore(q->irqlock, flags);
811 	retval = videobuf_waiton(q, q->read_buf, 0, 0);
812 	if (0 == retval) {
813 		CALL(q, sync, q, q->read_buf);
814 		if (VIDEOBUF_ERROR == q->read_buf->state)
815 			retval = -EIO;
816 		else
817 			retval = q->read_buf->size;
818 	}
819 
820 done:
821 	/* cleanup */
822 	q->ops->buf_release(q, q->read_buf);
823 	kfree(q->read_buf);
824 	q->read_buf = NULL;
825 	return retval;
826 }
827 
828 static int __videobuf_copy_to_user(struct videobuf_queue *q,
829 				   struct videobuf_buffer *buf,
830 				   char __user *data, size_t count,
831 				   int nonblocking)
832 {
833 	void *vaddr = CALL(q, vaddr, buf);
834 
835 	/* copy to userspace */
836 	if (count > buf->size - q->read_off)
837 		count = buf->size - q->read_off;
838 
839 	if (copy_to_user(data, vaddr + q->read_off, count))
840 		return -EFAULT;
841 
842 	return count;
843 }
844 
845 static int __videobuf_copy_stream(struct videobuf_queue *q,
846 				  struct videobuf_buffer *buf,
847 				  char __user *data, size_t count, size_t pos,
848 				  int vbihack, int nonblocking)
849 {
850 	unsigned int *fc = CALL(q, vaddr, buf);
851 
852 	if (vbihack) {
853 		/* dirty, undocumented hack -- pass the frame counter
854 			* within the last four bytes of each vbi data block.
855 			* We need that one to maintain backward compatibility
856 			* to all vbi decoding software out there ... */
857 		fc += (buf->size >> 2) - 1;
858 		*fc = buf->field_count >> 1;
859 		dprintk(1, "vbihack: %d\n", *fc);
860 	}
861 
862 	/* copy stuff using the common method */
863 	count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
864 
865 	if ((count == -EFAULT) && (pos == 0))
866 		return -EFAULT;
867 
868 	return count;
869 }
870 
871 ssize_t videobuf_read_one(struct videobuf_queue *q,
872 			  char __user *data, size_t count, loff_t *ppos,
873 			  int nonblocking)
874 {
875 	enum v4l2_field field;
876 	unsigned long flags = 0;
877 	unsigned size = 0, nbufs = 1;
878 	int retval;
879 
880 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
881 
882 	videobuf_queue_lock(q);
883 
884 	q->ops->buf_setup(q, &nbufs, &size);
885 
886 	if (NULL == q->read_buf  &&
887 	    count >= size        &&
888 	    !nonblocking) {
889 		retval = videobuf_read_zerocopy(q, data, count, ppos);
890 		if (retval >= 0  ||  retval == -EIO)
891 			/* ok, all done */
892 			goto done;
893 		/* fallback to kernel bounce buffer on failures */
894 	}
895 
896 	if (NULL == q->read_buf) {
897 		/* need to capture a new frame */
898 		retval = -ENOMEM;
899 		q->read_buf = videobuf_alloc_vb(q);
900 
901 		dprintk(1, "video alloc=0x%p\n", q->read_buf);
902 		if (NULL == q->read_buf)
903 			goto done;
904 		q->read_buf->memory = V4L2_MEMORY_USERPTR;
905 		q->read_buf->bsize = count; /* preferred size */
906 		field = videobuf_next_field(q);
907 		retval = q->ops->buf_prepare(q, q->read_buf, field);
908 
909 		if (0 != retval) {
910 			kfree(q->read_buf);
911 			q->read_buf = NULL;
912 			goto done;
913 		}
914 
915 		spin_lock_irqsave(q->irqlock, flags);
916 		q->ops->buf_queue(q, q->read_buf);
917 		spin_unlock_irqrestore(q->irqlock, flags);
918 
919 		q->read_off = 0;
920 	}
921 
922 	/* wait until capture is done */
923 	retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
924 	if (0 != retval)
925 		goto done;
926 
927 	CALL(q, sync, q, q->read_buf);
928 
929 	if (VIDEOBUF_ERROR == q->read_buf->state) {
930 		/* catch I/O errors */
931 		q->ops->buf_release(q, q->read_buf);
932 		kfree(q->read_buf);
933 		q->read_buf = NULL;
934 		retval = -EIO;
935 		goto done;
936 	}
937 
938 	/* Copy to userspace */
939 	retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
940 	if (retval < 0)
941 		goto done;
942 
943 	q->read_off += retval;
944 	if (q->read_off == q->read_buf->size) {
945 		/* all data copied, cleanup */
946 		q->ops->buf_release(q, q->read_buf);
947 		kfree(q->read_buf);
948 		q->read_buf = NULL;
949 	}
950 
951 done:
952 	videobuf_queue_unlock(q);
953 	return retval;
954 }
955 EXPORT_SYMBOL_GPL(videobuf_read_one);
956 
957 /* Locking: Caller holds q->vb_lock */
958 static int __videobuf_read_start(struct videobuf_queue *q)
959 {
960 	enum v4l2_field field;
961 	unsigned long flags = 0;
962 	unsigned int count = 0, size = 0;
963 	int err, i;
964 
965 	q->ops->buf_setup(q, &count, &size);
966 	if (count < 2)
967 		count = 2;
968 	if (count > VIDEO_MAX_FRAME)
969 		count = VIDEO_MAX_FRAME;
970 	size = PAGE_ALIGN(size);
971 
972 	err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
973 	if (err < 0)
974 		return err;
975 
976 	count = err;
977 
978 	for (i = 0; i < count; i++) {
979 		field = videobuf_next_field(q);
980 		err = q->ops->buf_prepare(q, q->bufs[i], field);
981 		if (err)
982 			return err;
983 		list_add_tail(&q->bufs[i]->stream, &q->stream);
984 	}
985 	spin_lock_irqsave(q->irqlock, flags);
986 	for (i = 0; i < count; i++)
987 		q->ops->buf_queue(q, q->bufs[i]);
988 	spin_unlock_irqrestore(q->irqlock, flags);
989 	q->reading = 1;
990 	return 0;
991 }
992 
993 static void __videobuf_read_stop(struct videobuf_queue *q)
994 {
995 	int i;
996 
997 	videobuf_queue_cancel(q);
998 	__videobuf_free(q);
999 	INIT_LIST_HEAD(&q->stream);
1000 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1001 		if (NULL == q->bufs[i])
1002 			continue;
1003 		kfree(q->bufs[i]);
1004 		q->bufs[i] = NULL;
1005 	}
1006 	q->read_buf = NULL;
1007 }
1008 
1009 int videobuf_read_start(struct videobuf_queue *q)
1010 {
1011 	int rc;
1012 
1013 	videobuf_queue_lock(q);
1014 	rc = __videobuf_read_start(q);
1015 	videobuf_queue_unlock(q);
1016 
1017 	return rc;
1018 }
1019 EXPORT_SYMBOL_GPL(videobuf_read_start);
1020 
1021 void videobuf_read_stop(struct videobuf_queue *q)
1022 {
1023 	videobuf_queue_lock(q);
1024 	__videobuf_read_stop(q);
1025 	videobuf_queue_unlock(q);
1026 }
1027 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1028 
1029 void videobuf_stop(struct videobuf_queue *q)
1030 {
1031 	videobuf_queue_lock(q);
1032 
1033 	if (q->streaming)
1034 		__videobuf_streamoff(q);
1035 
1036 	if (q->reading)
1037 		__videobuf_read_stop(q);
1038 
1039 	videobuf_queue_unlock(q);
1040 }
1041 EXPORT_SYMBOL_GPL(videobuf_stop);
1042 
1043 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1044 			     char __user *data, size_t count, loff_t *ppos,
1045 			     int vbihack, int nonblocking)
1046 {
1047 	int rc, retval;
1048 	unsigned long flags = 0;
1049 
1050 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1051 
1052 	dprintk(2, "%s\n", __func__);
1053 	videobuf_queue_lock(q);
1054 	retval = -EBUSY;
1055 	if (q->streaming)
1056 		goto done;
1057 	if (!q->reading) {
1058 		retval = __videobuf_read_start(q);
1059 		if (retval < 0)
1060 			goto done;
1061 	}
1062 
1063 	retval = 0;
1064 	while (count > 0) {
1065 		/* get / wait for data */
1066 		if (NULL == q->read_buf) {
1067 			q->read_buf = list_entry(q->stream.next,
1068 						 struct videobuf_buffer,
1069 						 stream);
1070 			list_del(&q->read_buf->stream);
1071 			q->read_off = 0;
1072 		}
1073 		rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
1074 		if (rc < 0) {
1075 			if (0 == retval)
1076 				retval = rc;
1077 			break;
1078 		}
1079 
1080 		if (q->read_buf->state == VIDEOBUF_DONE) {
1081 			rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
1082 					retval, vbihack, nonblocking);
1083 			if (rc < 0) {
1084 				retval = rc;
1085 				break;
1086 			}
1087 			retval      += rc;
1088 			count       -= rc;
1089 			q->read_off += rc;
1090 		} else {
1091 			/* some error */
1092 			q->read_off = q->read_buf->size;
1093 			if (0 == retval)
1094 				retval = -EIO;
1095 		}
1096 
1097 		/* requeue buffer when done with copying */
1098 		if (q->read_off == q->read_buf->size) {
1099 			list_add_tail(&q->read_buf->stream,
1100 				      &q->stream);
1101 			spin_lock_irqsave(q->irqlock, flags);
1102 			q->ops->buf_queue(q, q->read_buf);
1103 			spin_unlock_irqrestore(q->irqlock, flags);
1104 			q->read_buf = NULL;
1105 		}
1106 		if (retval < 0)
1107 			break;
1108 	}
1109 
1110 done:
1111 	videobuf_queue_unlock(q);
1112 	return retval;
1113 }
1114 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1115 
1116 unsigned int videobuf_poll_stream(struct file *file,
1117 				  struct videobuf_queue *q,
1118 				  poll_table *wait)
1119 {
1120 	unsigned long req_events = poll_requested_events(wait);
1121 	struct videobuf_buffer *buf = NULL;
1122 	unsigned int rc = 0;
1123 
1124 	videobuf_queue_lock(q);
1125 	if (q->streaming) {
1126 		if (!list_empty(&q->stream))
1127 			buf = list_entry(q->stream.next,
1128 					 struct videobuf_buffer, stream);
1129 	} else if (req_events & (POLLIN | POLLRDNORM)) {
1130 		if (!q->reading)
1131 			__videobuf_read_start(q);
1132 		if (!q->reading) {
1133 			rc = POLLERR;
1134 		} else if (NULL == q->read_buf) {
1135 			q->read_buf = list_entry(q->stream.next,
1136 						 struct videobuf_buffer,
1137 						 stream);
1138 			list_del(&q->read_buf->stream);
1139 			q->read_off = 0;
1140 		}
1141 		buf = q->read_buf;
1142 	}
1143 	if (!buf)
1144 		rc = POLLERR;
1145 
1146 	if (0 == rc) {
1147 		poll_wait(file, &buf->done, wait);
1148 		if (buf->state == VIDEOBUF_DONE ||
1149 		    buf->state == VIDEOBUF_ERROR) {
1150 			switch (q->type) {
1151 			case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1152 			case V4L2_BUF_TYPE_VBI_OUTPUT:
1153 			case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1154 				rc = POLLOUT | POLLWRNORM;
1155 				break;
1156 			default:
1157 				rc = POLLIN | POLLRDNORM;
1158 				break;
1159 			}
1160 		}
1161 	}
1162 	videobuf_queue_unlock(q);
1163 	return rc;
1164 }
1165 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1166 
1167 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1168 {
1169 	int rc = -EINVAL;
1170 	int i;
1171 
1172 	MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1173 
1174 	if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1175 		dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1176 		return -EINVAL;
1177 	}
1178 
1179 	videobuf_queue_lock(q);
1180 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1181 		struct videobuf_buffer *buf = q->bufs[i];
1182 
1183 		if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1184 				buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1185 			rc = CALL(q, mmap_mapper, q, buf, vma);
1186 			break;
1187 		}
1188 	}
1189 	videobuf_queue_unlock(q);
1190 
1191 	return rc;
1192 }
1193 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1194