OMAP3: PM: decouple PER and CORE context save and restore
[linux-ginger.git] / drivers / media / video / videobuf-core.c
blob8e93c6f25c83ae4cab47bc715712b56ff5aa059a
1 /*
2 * generic helper functions for handling video4linux capture buffers
4 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
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
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
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>
24 #include <media/videobuf-core.h>
26 #define MAGIC_BUFFER 0x20070728
27 #define MAGIC_CHECK(is, should) do { \
28 if (unlikely((is) != (should))) { \
29 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
30 BUG(); } } while (0)
32 static int debug;
33 module_param(debug, int, 0644);
35 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
36 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
37 MODULE_LICENSE("GPL");
39 #define dprintk(level, fmt, arg...) do { \
40 if (debug >= level) \
41 printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
43 /* --------------------------------------------------------------------- */
45 #define CALL(q, f, arg...) \
46 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
48 void *videobuf_alloc(struct videobuf_queue *q)
50 struct videobuf_buffer *vb;
52 BUG_ON(q->msize < sizeof(*vb));
54 if (!q->int_ops || !q->int_ops->alloc) {
55 printk(KERN_ERR "No specific ops defined!\n");
56 BUG();
59 vb = q->int_ops->alloc(q->msize);
61 if (NULL != vb) {
62 init_waitqueue_head(&vb->done);
63 vb->magic = MAGIC_BUFFER;
66 return vb;
69 #define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
70 vb->state != VIDEOBUF_QUEUED)
71 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
73 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
75 if (non_blocking) {
76 if (WAITON_CONDITION)
77 return 0;
78 else
79 return -EAGAIN;
82 if (intr)
83 return wait_event_interruptible(vb->done, WAITON_CONDITION);
84 else
85 wait_event(vb->done, WAITON_CONDITION);
87 return 0;
90 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
91 struct v4l2_framebuffer *fbuf)
93 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
94 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
96 return CALL(q, iolock, q, vb, fbuf);
99 void *videobuf_queue_to_vmalloc (struct videobuf_queue *q,
100 struct videobuf_buffer *buf)
102 if (q->int_ops->vmalloc)
103 return q->int_ops->vmalloc(buf);
104 else
105 return NULL;
107 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
109 /* --------------------------------------------------------------------- */
112 void videobuf_queue_core_init(struct videobuf_queue *q,
113 struct videobuf_queue_ops *ops,
114 struct device *dev,
115 spinlock_t *irqlock,
116 enum v4l2_buf_type type,
117 enum v4l2_field field,
118 unsigned int msize,
119 void *priv,
120 struct videobuf_qtype_ops *int_ops)
122 BUG_ON(!q);
123 memset(q, 0, sizeof(*q));
124 q->irqlock = irqlock;
125 q->dev = dev;
126 q->type = type;
127 q->field = field;
128 q->msize = msize;
129 q->ops = ops;
130 q->priv_data = priv;
131 q->int_ops = int_ops;
133 /* All buffer operations are mandatory */
134 BUG_ON(!q->ops->buf_setup);
135 BUG_ON(!q->ops->buf_prepare);
136 BUG_ON(!q->ops->buf_queue);
137 BUG_ON(!q->ops->buf_release);
139 /* Lock is mandatory for queue_cancel to work */
140 BUG_ON(!irqlock);
142 /* Having implementations for abstract methods are mandatory */
143 BUG_ON(!q->int_ops);
145 mutex_init(&q->vb_lock);
146 init_waitqueue_head(&q->wait);
147 INIT_LIST_HEAD(&q->stream);
150 /* Locking: Only usage in bttv unsafe find way to remove */
151 int videobuf_queue_is_busy(struct videobuf_queue *q)
153 int i;
155 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
157 if (q->streaming) {
158 dprintk(1, "busy: streaming active\n");
159 return 1;
161 if (q->reading) {
162 dprintk(1, "busy: pending read #1\n");
163 return 1;
165 if (q->read_buf) {
166 dprintk(1, "busy: pending read #2\n");
167 return 1;
169 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
170 if (NULL == q->bufs[i])
171 continue;
172 if (q->bufs[i]->map) {
173 dprintk(1, "busy: buffer #%d mapped\n", i);
174 return 1;
176 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
177 dprintk(1, "busy: buffer #%d queued\n", i);
178 return 1;
180 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
181 dprintk(1, "busy: buffer #%d avtive\n", i);
182 return 1;
185 return 0;
188 /* Locking: Caller holds q->vb_lock */
189 void videobuf_queue_cancel(struct videobuf_queue *q)
191 unsigned long flags = 0;
192 int i;
194 q->streaming = 0;
195 q->reading = 0;
196 wake_up_interruptible_sync(&q->wait);
198 /* remove queued buffers from list */
199 spin_lock_irqsave(q->irqlock, flags);
200 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
201 if (NULL == q->bufs[i])
202 continue;
203 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
204 list_del(&q->bufs[i]->queue);
205 q->bufs[i]->state = VIDEOBUF_ERROR;
206 wake_up_all(&q->bufs[i]->done);
209 spin_unlock_irqrestore(q->irqlock, flags);
211 /* free all buffers + clear queue */
212 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
213 if (NULL == q->bufs[i])
214 continue;
215 q->ops->buf_release(q, q->bufs[i]);
217 INIT_LIST_HEAD(&q->stream);
220 /* --------------------------------------------------------------------- */
222 /* Locking: Caller holds q->vb_lock */
223 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
225 enum v4l2_field field = q->field;
227 BUG_ON(V4L2_FIELD_ANY == field);
229 if (V4L2_FIELD_ALTERNATE == field) {
230 if (V4L2_FIELD_TOP == q->last) {
231 field = V4L2_FIELD_BOTTOM;
232 q->last = V4L2_FIELD_BOTTOM;
233 } else {
234 field = V4L2_FIELD_TOP;
235 q->last = V4L2_FIELD_TOP;
238 return field;
241 /* Locking: Caller holds q->vb_lock */
242 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
243 struct videobuf_buffer *vb, enum v4l2_buf_type type)
245 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
246 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
248 b->index = vb->i;
249 b->type = type;
251 b->memory = vb->memory;
252 switch (b->memory) {
253 case V4L2_MEMORY_MMAP:
254 b->m.offset = vb->boff;
255 b->length = vb->bsize;
256 break;
257 case V4L2_MEMORY_USERPTR:
258 b->m.userptr = vb->baddr;
259 b->length = vb->bsize;
260 break;
261 case V4L2_MEMORY_OVERLAY:
262 b->m.offset = vb->boff;
263 break;
266 b->flags = 0;
267 if (vb->map)
268 b->flags |= V4L2_BUF_FLAG_MAPPED;
270 switch (vb->state) {
271 case VIDEOBUF_PREPARED:
272 case VIDEOBUF_QUEUED:
273 case VIDEOBUF_ACTIVE:
274 b->flags |= V4L2_BUF_FLAG_QUEUED;
275 break;
276 case VIDEOBUF_DONE:
277 case VIDEOBUF_ERROR:
278 b->flags |= V4L2_BUF_FLAG_DONE;
279 break;
280 case VIDEOBUF_NEEDS_INIT:
281 case VIDEOBUF_IDLE:
282 /* nothing */
283 break;
286 if (vb->input != UNSET) {
287 b->flags |= V4L2_BUF_FLAG_INPUT;
288 b->input = vb->input;
291 b->field = vb->field;
292 b->timestamp = vb->ts;
293 b->bytesused = vb->size;
294 b->sequence = vb->field_count >> 1;
297 /* Locking: Caller holds q->vb_lock */
298 static int __videobuf_mmap_free(struct videobuf_queue *q)
300 int i;
301 int rc;
303 if (!q)
304 return 0;
306 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
309 rc = CALL(q, mmap_free, q);
311 q->is_mmapped = 0;
313 if (rc < 0)
314 return rc;
316 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
317 if (NULL == q->bufs[i])
318 continue;
319 q->ops->buf_release(q, q->bufs[i]);
320 kfree(q->bufs[i]);
321 q->bufs[i] = NULL;
324 return rc;
327 int videobuf_mmap_free(struct videobuf_queue *q)
329 int ret;
330 mutex_lock(&q->vb_lock);
331 ret = __videobuf_mmap_free(q);
332 mutex_unlock(&q->vb_lock);
333 return ret;
336 /* Locking: Caller holds q->vb_lock */
337 int __videobuf_mmap_setup(struct videobuf_queue *q,
338 unsigned int bcount, unsigned int bsize,
339 enum v4l2_memory memory)
341 unsigned int i;
342 int err;
344 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
346 err = __videobuf_mmap_free(q);
347 if (0 != err)
348 return err;
350 /* Allocate and initialize buffers */
351 for (i = 0; i < bcount; i++) {
352 q->bufs[i] = videobuf_alloc(q);
354 if (q->bufs[i] == NULL)
355 break;
357 q->bufs[i]->i = i;
358 q->bufs[i]->input = UNSET;
359 q->bufs[i]->memory = memory;
360 q->bufs[i]->bsize = bsize;
361 switch (memory) {
362 case V4L2_MEMORY_MMAP:
363 q->bufs[i]->boff = bsize * i;
364 break;
365 case V4L2_MEMORY_USERPTR:
366 case V4L2_MEMORY_OVERLAY:
367 /* nothing */
368 break;
372 if (!i)
373 return -ENOMEM;
375 dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
376 i, bsize);
378 return i;
381 int videobuf_mmap_setup(struct videobuf_queue *q,
382 unsigned int bcount, unsigned int bsize,
383 enum v4l2_memory memory)
385 int ret;
386 mutex_lock(&q->vb_lock);
387 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
388 mutex_unlock(&q->vb_lock);
389 return ret;
392 int videobuf_reqbufs(struct videobuf_queue *q,
393 struct v4l2_requestbuffers *req)
395 unsigned int size, count;
396 int retval;
398 if (req->count < 1) {
399 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
400 return -EINVAL;
403 if (req->memory != V4L2_MEMORY_MMAP &&
404 req->memory != V4L2_MEMORY_USERPTR &&
405 req->memory != V4L2_MEMORY_OVERLAY) {
406 dprintk(1, "reqbufs: memory type invalid\n");
407 return -EINVAL;
410 mutex_lock(&q->vb_lock);
411 if (req->type != q->type) {
412 dprintk(1, "reqbufs: queue type invalid\n");
413 retval = -EINVAL;
414 goto done;
417 if (q->streaming) {
418 dprintk(1, "reqbufs: streaming already exists\n");
419 retval = -EBUSY;
420 goto done;
422 if (!list_empty(&q->stream)) {
423 dprintk(1, "reqbufs: stream running\n");
424 retval = -EBUSY;
425 goto done;
428 count = req->count;
429 if (count > VIDEO_MAX_FRAME)
430 count = VIDEO_MAX_FRAME;
431 size = 0;
432 q->ops->buf_setup(q, &count, &size);
433 size = PAGE_ALIGN(size);
434 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
435 count, size, (count*size)>>PAGE_SHIFT);
437 retval = __videobuf_mmap_setup(q, count, size, req->memory);
438 if (retval < 0) {
439 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
440 goto done;
443 req->count = retval;
444 retval = 0;
446 done:
447 mutex_unlock(&q->vb_lock);
448 return retval;
451 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
453 int ret = -EINVAL;
455 mutex_lock(&q->vb_lock);
456 if (unlikely(b->type != q->type)) {
457 dprintk(1, "querybuf: Wrong type.\n");
458 goto done;
460 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
461 dprintk(1, "querybuf: index out of range.\n");
462 goto done;
464 if (unlikely(NULL == q->bufs[b->index])) {
465 dprintk(1, "querybuf: buffer is null.\n");
466 goto done;
469 videobuf_status(q, b, q->bufs[b->index], q->type);
471 ret = 0;
472 done:
473 mutex_unlock(&q->vb_lock);
474 return ret;
477 int videobuf_qbuf(struct videobuf_queue *q,
478 struct v4l2_buffer *b)
480 struct videobuf_buffer *buf;
481 enum v4l2_field field;
482 unsigned long flags = 0;
483 int retval;
485 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
487 if (b->memory == V4L2_MEMORY_MMAP)
488 down_read(&current->mm->mmap_sem);
490 mutex_lock(&q->vb_lock);
491 retval = -EBUSY;
492 if (q->reading) {
493 dprintk(1, "qbuf: Reading running...\n");
494 goto done;
496 retval = -EINVAL;
497 if (b->type != q->type) {
498 dprintk(1, "qbuf: Wrong type.\n");
499 goto done;
501 if (b->index >= VIDEO_MAX_FRAME) {
502 dprintk(1, "qbuf: index out of range.\n");
503 goto done;
505 buf = q->bufs[b->index];
506 if (NULL == buf) {
507 dprintk(1, "qbuf: buffer is null.\n");
508 goto done;
510 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
511 if (buf->memory != b->memory) {
512 dprintk(1, "qbuf: memory type is wrong.\n");
513 goto done;
515 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
516 dprintk(1, "qbuf: buffer is already queued or active.\n");
517 goto done;
520 if (b->flags & V4L2_BUF_FLAG_INPUT) {
521 if (b->input >= q->inputs) {
522 dprintk(1, "qbuf: wrong input.\n");
523 goto done;
525 buf->input = b->input;
526 } else {
527 buf->input = UNSET;
530 switch (b->memory) {
531 case V4L2_MEMORY_MMAP:
532 if (0 == buf->baddr) {
533 dprintk(1, "qbuf: mmap requested "
534 "but buffer addr is zero!\n");
535 goto done;
537 break;
538 case V4L2_MEMORY_USERPTR:
539 if (b->length < buf->bsize) {
540 dprintk(1, "qbuf: buffer length is not enough\n");
541 goto done;
543 if (VIDEOBUF_NEEDS_INIT != buf->state &&
544 buf->baddr != b->m.userptr)
545 q->ops->buf_release(q, buf);
546 buf->baddr = b->m.userptr;
547 break;
548 case V4L2_MEMORY_OVERLAY:
549 buf->boff = b->m.offset;
550 break;
551 default:
552 dprintk(1, "qbuf: wrong memory type\n");
553 goto done;
556 dprintk(1, "qbuf: requesting next field\n");
557 field = videobuf_next_field(q);
558 retval = q->ops->buf_prepare(q, buf, field);
559 if (0 != retval) {
560 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
561 goto done;
564 list_add_tail(&buf->stream, &q->stream);
565 if (q->streaming) {
566 spin_lock_irqsave(q->irqlock, flags);
567 q->ops->buf_queue(q, buf);
568 spin_unlock_irqrestore(q->irqlock, flags);
570 dprintk(1, "qbuf: succeded\n");
571 retval = 0;
572 wake_up_interruptible_sync(&q->wait);
574 done:
575 mutex_unlock(&q->vb_lock);
577 if (b->memory == V4L2_MEMORY_MMAP)
578 up_read(&current->mm->mmap_sem);
580 return retval;
584 /* Locking: Caller holds q->vb_lock */
585 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
587 int retval;
589 checks:
590 if (!q->streaming) {
591 dprintk(1, "next_buffer: Not streaming\n");
592 retval = -EINVAL;
593 goto done;
596 if (list_empty(&q->stream)) {
597 if (noblock) {
598 retval = -EAGAIN;
599 dprintk(2, "next_buffer: no buffers to dequeue\n");
600 goto done;
601 } else {
602 dprintk(2, "next_buffer: waiting on buffer\n");
604 /* Drop lock to avoid deadlock with qbuf */
605 mutex_unlock(&q->vb_lock);
607 /* Checking list_empty and streaming is safe without
608 * locks because we goto checks to validate while
609 * holding locks before proceeding */
610 retval = wait_event_interruptible(q->wait,
611 !list_empty(&q->stream) || !q->streaming);
612 mutex_lock(&q->vb_lock);
614 if (retval)
615 goto done;
617 goto checks;
621 retval = 0;
623 done:
624 return retval;
628 /* Locking: Caller holds q->vb_lock */
629 static int stream_next_buffer(struct videobuf_queue *q,
630 struct videobuf_buffer **vb, int nonblocking)
632 int retval;
633 struct videobuf_buffer *buf = NULL;
635 retval = stream_next_buffer_check_queue(q, nonblocking);
636 if (retval)
637 goto done;
639 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
640 retval = videobuf_waiton(buf, nonblocking, 1);
641 if (retval < 0)
642 goto done;
644 *vb = buf;
645 done:
646 return retval;
649 int videobuf_dqbuf(struct videobuf_queue *q,
650 struct v4l2_buffer *b, int nonblocking)
652 struct videobuf_buffer *buf = NULL;
653 int retval;
655 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
657 mutex_lock(&q->vb_lock);
659 retval = stream_next_buffer(q, &buf, nonblocking);
660 if (retval < 0) {
661 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
662 goto done;
665 switch (buf->state) {
666 case VIDEOBUF_ERROR:
667 dprintk(1, "dqbuf: state is error\n");
668 retval = -EIO;
669 CALL(q, sync, q, buf);
670 buf->state = VIDEOBUF_IDLE;
671 break;
672 case VIDEOBUF_DONE:
673 dprintk(1, "dqbuf: state is done\n");
674 CALL(q, sync, q, buf);
675 buf->state = VIDEOBUF_IDLE;
676 break;
677 default:
678 dprintk(1, "dqbuf: state invalid\n");
679 retval = -EINVAL;
680 goto done;
682 list_del(&buf->stream);
683 memset(b, 0, sizeof(*b));
684 videobuf_status(q, b, buf, q->type);
686 done:
687 mutex_unlock(&q->vb_lock);
688 return retval;
691 int videobuf_streamon(struct videobuf_queue *q)
693 struct videobuf_buffer *buf;
694 unsigned long flags = 0;
695 int retval;
697 mutex_lock(&q->vb_lock);
698 retval = -EBUSY;
699 if (q->reading)
700 goto done;
701 retval = 0;
702 if (q->streaming)
703 goto done;
704 q->streaming = 1;
705 spin_lock_irqsave(q->irqlock, flags);
706 list_for_each_entry(buf, &q->stream, stream)
707 if (buf->state == VIDEOBUF_PREPARED)
708 q->ops->buf_queue(q, buf);
709 spin_unlock_irqrestore(q->irqlock, flags);
711 wake_up_interruptible_sync(&q->wait);
712 done:
713 mutex_unlock(&q->vb_lock);
714 return retval;
717 /* Locking: Caller holds q->vb_lock */
718 static int __videobuf_streamoff(struct videobuf_queue *q)
720 if (!q->streaming)
721 return -EINVAL;
723 videobuf_queue_cancel(q);
725 return 0;
728 int videobuf_streamoff(struct videobuf_queue *q)
730 int retval;
732 mutex_lock(&q->vb_lock);
733 retval = __videobuf_streamoff(q);
734 mutex_unlock(&q->vb_lock);
736 return retval;
739 /* Locking: Caller holds q->vb_lock */
740 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
741 char __user *data,
742 size_t count, loff_t *ppos)
744 enum v4l2_field field;
745 unsigned long flags = 0;
746 int retval;
748 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
750 /* setup stuff */
751 q->read_buf = videobuf_alloc(q);
752 if (NULL == q->read_buf)
753 return -ENOMEM;
755 q->read_buf->memory = V4L2_MEMORY_USERPTR;
756 q->read_buf->baddr = (unsigned long)data;
757 q->read_buf->bsize = count;
759 field = videobuf_next_field(q);
760 retval = q->ops->buf_prepare(q, q->read_buf, field);
761 if (0 != retval)
762 goto done;
764 /* start capture & wait */
765 spin_lock_irqsave(q->irqlock, flags);
766 q->ops->buf_queue(q, q->read_buf);
767 spin_unlock_irqrestore(q->irqlock, flags);
768 retval = videobuf_waiton(q->read_buf, 0, 0);
769 if (0 == retval) {
770 CALL(q, sync, q, q->read_buf);
771 if (VIDEOBUF_ERROR == q->read_buf->state)
772 retval = -EIO;
773 else
774 retval = q->read_buf->size;
777 done:
778 /* cleanup */
779 q->ops->buf_release(q, q->read_buf);
780 kfree(q->read_buf);
781 q->read_buf = NULL;
782 return retval;
785 ssize_t videobuf_read_one(struct videobuf_queue *q,
786 char __user *data, size_t count, loff_t *ppos,
787 int nonblocking)
789 enum v4l2_field field;
790 unsigned long flags = 0;
791 unsigned size = 0, nbufs = 1;
792 int retval;
794 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
796 mutex_lock(&q->vb_lock);
798 q->ops->buf_setup(q, &nbufs, &size);
800 if (NULL == q->read_buf &&
801 count >= size &&
802 !nonblocking) {
803 retval = videobuf_read_zerocopy(q, data, count, ppos);
804 if (retval >= 0 || retval == -EIO)
805 /* ok, all done */
806 goto done;
807 /* fallback to kernel bounce buffer on failures */
810 if (NULL == q->read_buf) {
811 /* need to capture a new frame */
812 retval = -ENOMEM;
813 q->read_buf = videobuf_alloc(q);
815 dprintk(1, "video alloc=0x%p\n", q->read_buf);
816 if (NULL == q->read_buf)
817 goto done;
818 q->read_buf->memory = V4L2_MEMORY_USERPTR;
819 q->read_buf->bsize = count; /* preferred size */
820 field = videobuf_next_field(q);
821 retval = q->ops->buf_prepare(q, q->read_buf, field);
823 if (0 != retval) {
824 kfree(q->read_buf);
825 q->read_buf = NULL;
826 goto done;
829 spin_lock_irqsave(q->irqlock, flags);
830 q->ops->buf_queue(q, q->read_buf);
831 spin_unlock_irqrestore(q->irqlock, flags);
833 q->read_off = 0;
836 /* wait until capture is done */
837 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
838 if (0 != retval)
839 goto done;
841 CALL(q, sync, q, q->read_buf);
843 if (VIDEOBUF_ERROR == q->read_buf->state) {
844 /* catch I/O errors */
845 q->ops->buf_release(q, q->read_buf);
846 kfree(q->read_buf);
847 q->read_buf = NULL;
848 retval = -EIO;
849 goto done;
852 /* Copy to userspace */
853 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
854 if (retval < 0)
855 goto done;
857 q->read_off += retval;
858 if (q->read_off == q->read_buf->size) {
859 /* all data copied, cleanup */
860 q->ops->buf_release(q, q->read_buf);
861 kfree(q->read_buf);
862 q->read_buf = NULL;
865 done:
866 mutex_unlock(&q->vb_lock);
867 return retval;
870 /* Locking: Caller holds q->vb_lock */
871 static int __videobuf_read_start(struct videobuf_queue *q)
873 enum v4l2_field field;
874 unsigned long flags = 0;
875 unsigned int count = 0, size = 0;
876 int err, i;
878 q->ops->buf_setup(q, &count, &size);
879 if (count < 2)
880 count = 2;
881 if (count > VIDEO_MAX_FRAME)
882 count = VIDEO_MAX_FRAME;
883 size = PAGE_ALIGN(size);
885 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
886 if (err < 0)
887 return err;
889 count = err;
891 for (i = 0; i < count; i++) {
892 field = videobuf_next_field(q);
893 err = q->ops->buf_prepare(q, q->bufs[i], field);
894 if (err)
895 return err;
896 list_add_tail(&q->bufs[i]->stream, &q->stream);
898 spin_lock_irqsave(q->irqlock, flags);
899 for (i = 0; i < count; i++)
900 q->ops->buf_queue(q, q->bufs[i]);
901 spin_unlock_irqrestore(q->irqlock, flags);
902 q->reading = 1;
903 return 0;
906 static void __videobuf_read_stop(struct videobuf_queue *q)
908 int i;
910 videobuf_queue_cancel(q);
911 __videobuf_mmap_free(q);
912 INIT_LIST_HEAD(&q->stream);
913 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
914 if (NULL == q->bufs[i])
915 continue;
916 kfree(q->bufs[i]);
917 q->bufs[i] = NULL;
919 q->read_buf = NULL;
923 int videobuf_read_start(struct videobuf_queue *q)
925 int rc;
927 mutex_lock(&q->vb_lock);
928 rc = __videobuf_read_start(q);
929 mutex_unlock(&q->vb_lock);
931 return rc;
934 void videobuf_read_stop(struct videobuf_queue *q)
936 mutex_lock(&q->vb_lock);
937 __videobuf_read_stop(q);
938 mutex_unlock(&q->vb_lock);
941 void videobuf_stop(struct videobuf_queue *q)
943 mutex_lock(&q->vb_lock);
945 if (q->streaming)
946 __videobuf_streamoff(q);
948 if (q->reading)
949 __videobuf_read_stop(q);
951 mutex_unlock(&q->vb_lock);
955 ssize_t videobuf_read_stream(struct videobuf_queue *q,
956 char __user *data, size_t count, loff_t *ppos,
957 int vbihack, int nonblocking)
959 int rc, retval;
960 unsigned long flags = 0;
962 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
964 dprintk(2, "%s\n", __func__);
965 mutex_lock(&q->vb_lock);
966 retval = -EBUSY;
967 if (q->streaming)
968 goto done;
969 if (!q->reading) {
970 retval = __videobuf_read_start(q);
971 if (retval < 0)
972 goto done;
975 retval = 0;
976 while (count > 0) {
977 /* get / wait for data */
978 if (NULL == q->read_buf) {
979 q->read_buf = list_entry(q->stream.next,
980 struct videobuf_buffer,
981 stream);
982 list_del(&q->read_buf->stream);
983 q->read_off = 0;
985 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
986 if (rc < 0) {
987 if (0 == retval)
988 retval = rc;
989 break;
992 if (q->read_buf->state == VIDEOBUF_DONE) {
993 rc = CALL(q, copy_stream, q, data + retval, count,
994 retval, vbihack, nonblocking);
995 if (rc < 0) {
996 retval = rc;
997 break;
999 retval += rc;
1000 count -= rc;
1001 q->read_off += rc;
1002 } else {
1003 /* some error */
1004 q->read_off = q->read_buf->size;
1005 if (0 == retval)
1006 retval = -EIO;
1009 /* requeue buffer when done with copying */
1010 if (q->read_off == q->read_buf->size) {
1011 list_add_tail(&q->read_buf->stream,
1012 &q->stream);
1013 spin_lock_irqsave(q->irqlock, flags);
1014 q->ops->buf_queue(q, q->read_buf);
1015 spin_unlock_irqrestore(q->irqlock, flags);
1016 q->read_buf = NULL;
1018 if (retval < 0)
1019 break;
1022 done:
1023 mutex_unlock(&q->vb_lock);
1024 return retval;
1027 unsigned int videobuf_poll_stream(struct file *file,
1028 struct videobuf_queue *q,
1029 poll_table *wait)
1031 struct videobuf_buffer *buf = NULL;
1032 unsigned int rc = 0;
1034 mutex_lock(&q->vb_lock);
1035 if (q->streaming) {
1036 if (!list_empty(&q->stream))
1037 buf = list_entry(q->stream.next,
1038 struct videobuf_buffer, stream);
1039 } else {
1040 if (!q->reading)
1041 __videobuf_read_start(q);
1042 if (!q->reading) {
1043 rc = POLLERR;
1044 } else if (NULL == q->read_buf) {
1045 q->read_buf = list_entry(q->stream.next,
1046 struct videobuf_buffer,
1047 stream);
1048 list_del(&q->read_buf->stream);
1049 q->read_off = 0;
1051 buf = q->read_buf;
1053 if (!buf)
1054 rc = POLLERR;
1056 if (0 == rc) {
1057 poll_wait(file, &buf->done, wait);
1058 if (buf->state == VIDEOBUF_DONE ||
1059 buf->state == VIDEOBUF_ERROR)
1060 rc = POLLIN|POLLRDNORM;
1062 mutex_unlock(&q->vb_lock);
1063 return rc;
1066 int videobuf_mmap_mapper(struct videobuf_queue *q,
1067 struct vm_area_struct *vma)
1069 int retval;
1071 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1073 mutex_lock(&q->vb_lock);
1074 retval = CALL(q, mmap_mapper, q, vma);
1075 q->is_mmapped = 1;
1076 mutex_unlock(&q->vb_lock);
1078 return retval;
1081 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1082 int videobuf_cgmbuf(struct videobuf_queue *q,
1083 struct video_mbuf *mbuf, int count)
1085 struct v4l2_requestbuffers req;
1086 int rc, i;
1088 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1090 memset(&req, 0, sizeof(req));
1091 req.type = q->type;
1092 req.count = count;
1093 req.memory = V4L2_MEMORY_MMAP;
1094 rc = videobuf_reqbufs(q, &req);
1095 if (rc < 0)
1096 return rc;
1098 mbuf->frames = req.count;
1099 mbuf->size = 0;
1100 for (i = 0; i < mbuf->frames; i++) {
1101 mbuf->offsets[i] = q->bufs[i]->boff;
1102 mbuf->size += q->bufs[i]->bsize;
1105 return 0;
1107 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1108 #endif
1110 /* --------------------------------------------------------------------- */
1112 EXPORT_SYMBOL_GPL(videobuf_waiton);
1113 EXPORT_SYMBOL_GPL(videobuf_iolock);
1115 EXPORT_SYMBOL_GPL(videobuf_alloc);
1117 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
1118 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1119 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1121 EXPORT_SYMBOL_GPL(videobuf_next_field);
1122 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1123 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1124 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1125 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1126 EXPORT_SYMBOL_GPL(videobuf_streamon);
1127 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1129 EXPORT_SYMBOL_GPL(videobuf_read_start);
1130 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1131 EXPORT_SYMBOL_GPL(videobuf_stop);
1132 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1133 EXPORT_SYMBOL_GPL(videobuf_read_one);
1134 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1136 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
1137 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1138 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1139 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);