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