V4L/DVB (6278): Buf: fix typo that caused data loss when readng streams from device
[linux-2.6/mini2440.git] / drivers / media / video / videobuf-core.c
blob8e4026e6fae3c62b2d16ae24ad5b7bbc8c772a3c
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/slab.h>
20 #include <linux/interrupt.h>
22 #include <media/videobuf-core.h>
24 #define MAGIC_BUFFER 0x20070728
25 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
26 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
28 static int debug = 0;
29 module_param(debug, int, 0644);
31 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
32 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
33 MODULE_LICENSE("GPL");
35 #define dprintk(level, fmt, arg...) if (debug >= level) \
36 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
38 /* --------------------------------------------------------------------- */
40 #define CALL(q, f, arg...) \
41 ( (q->int_ops->f)? q->int_ops->f(arg) : 0)
43 void* videobuf_alloc(struct videobuf_queue* q)
45 struct videobuf_buffer *vb;
47 BUG_ON (q->msize<sizeof(*vb));
49 if (!q->int_ops || !q->int_ops->alloc) {
50 printk(KERN_ERR "No specific ops defined!\n");
51 BUG();
54 vb = q->int_ops->alloc(q->msize);
56 if (NULL != vb) {
57 init_waitqueue_head(&vb->done);
58 vb->magic = MAGIC_BUFFER;
61 return vb;
64 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
66 int retval = 0;
67 DECLARE_WAITQUEUE(wait, current);
69 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
70 add_wait_queue(&vb->done, &wait);
71 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
72 if (non_blocking) {
73 retval = -EAGAIN;
74 break;
76 set_current_state(intr ? TASK_INTERRUPTIBLE
77 : TASK_UNINTERRUPTIBLE);
78 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
79 schedule();
80 set_current_state(TASK_RUNNING);
81 if (intr && signal_pending(current)) {
82 dprintk(1,"buffer waiton: -EINTR\n");
83 retval = -EINTR;
84 break;
87 remove_wait_queue(&vb->done, &wait);
88 return retval;
91 int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
92 struct v4l2_framebuffer *fbuf)
94 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
95 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
97 return CALL(q,iolock,q,vb,fbuf);
100 /* --------------------------------------------------------------------- */
103 void videobuf_queue_init(struct videobuf_queue* q,
104 struct videobuf_queue_ops *ops,
105 void *dev,
106 spinlock_t *irqlock,
107 enum v4l2_buf_type type,
108 enum v4l2_field field,
109 unsigned int msize,
110 void *priv)
112 memset(q,0,sizeof(*q));
113 q->irqlock = irqlock;
114 q->dev = dev;
115 q->type = type;
116 q->field = field;
117 q->msize = msize;
118 q->ops = ops;
119 q->priv_data = priv;
121 /* All buffer operations are mandatory */
122 BUG_ON (!q->ops->buf_setup);
123 BUG_ON (!q->ops->buf_prepare);
124 BUG_ON (!q->ops->buf_queue);
125 BUG_ON (!q->ops->buf_release);
127 mutex_init(&q->lock);
128 INIT_LIST_HEAD(&q->stream);
131 int videobuf_queue_is_busy(struct videobuf_queue *q)
133 int i;
135 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
137 if (q->streaming) {
138 dprintk(1,"busy: streaming active\n");
139 return 1;
141 if (q->reading) {
142 dprintk(1,"busy: pending read #1\n");
143 return 1;
145 if (q->read_buf) {
146 dprintk(1,"busy: pending read #2\n");
147 return 1;
149 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
150 if (NULL == q->bufs[i])
151 continue;
152 if (q->bufs[i]->map) {
153 dprintk(1,"busy: buffer #%d mapped\n",i);
154 return 1;
156 if (q->bufs[i]->state == STATE_QUEUED) {
157 dprintk(1,"busy: buffer #%d queued\n",i);
158 return 1;
160 if (q->bufs[i]->state == STATE_ACTIVE) {
161 dprintk(1,"busy: buffer #%d avtive\n",i);
162 return 1;
165 return 0;
168 void videobuf_queue_cancel(struct videobuf_queue *q)
170 unsigned long flags=0;
171 int i;
173 /* remove queued buffers from list */
174 if (q->irqlock)
175 spin_lock_irqsave(q->irqlock,flags);
176 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
177 if (NULL == q->bufs[i])
178 continue;
179 if (q->bufs[i]->state == STATE_QUEUED) {
180 list_del(&q->bufs[i]->queue);
181 q->bufs[i]->state = STATE_ERROR;
184 if (q->irqlock)
185 spin_unlock_irqrestore(q->irqlock,flags);
187 /* free all buffers + clear queue */
188 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
189 if (NULL == q->bufs[i])
190 continue;
191 q->ops->buf_release(q,q->bufs[i]);
193 INIT_LIST_HEAD(&q->stream);
196 /* --------------------------------------------------------------------- */
198 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
200 enum v4l2_field field = q->field;
202 BUG_ON(V4L2_FIELD_ANY == field);
204 if (V4L2_FIELD_ALTERNATE == field) {
205 if (V4L2_FIELD_TOP == q->last) {
206 field = V4L2_FIELD_BOTTOM;
207 q->last = V4L2_FIELD_BOTTOM;
208 } else {
209 field = V4L2_FIELD_TOP;
210 q->last = V4L2_FIELD_TOP;
213 return field;
216 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
217 struct videobuf_buffer *vb, enum v4l2_buf_type type)
219 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
220 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
222 b->index = vb->i;
223 b->type = type;
225 b->memory = vb->memory;
226 switch (b->memory) {
227 case V4L2_MEMORY_MMAP:
228 b->m.offset = vb->boff;
229 b->length = vb->bsize;
230 break;
231 case V4L2_MEMORY_USERPTR:
232 b->m.userptr = vb->baddr;
233 b->length = vb->bsize;
234 break;
235 case V4L2_MEMORY_OVERLAY:
236 b->m.offset = vb->boff;
237 break;
240 b->flags = 0;
241 if (vb->map)
242 b->flags |= V4L2_BUF_FLAG_MAPPED;
244 switch (vb->state) {
245 case STATE_PREPARED:
246 case STATE_QUEUED:
247 case STATE_ACTIVE:
248 b->flags |= V4L2_BUF_FLAG_QUEUED;
249 break;
250 case STATE_DONE:
251 case STATE_ERROR:
252 b->flags |= V4L2_BUF_FLAG_DONE;
253 break;
254 case STATE_NEEDS_INIT:
255 case STATE_IDLE:
256 /* nothing */
257 break;
260 if (vb->input != UNSET) {
261 b->flags |= V4L2_BUF_FLAG_INPUT;
262 b->input = vb->input;
265 b->field = vb->field;
266 b->timestamp = vb->ts;
267 b->bytesused = vb->size;
268 b->sequence = vb->field_count >> 1;
271 int videobuf_reqbufs(struct videobuf_queue *q,
272 struct v4l2_requestbuffers *req)
274 unsigned int size,count;
275 int retval;
277 if (req->type != q->type) {
278 dprintk(1,"reqbufs: queue type invalid\n");
279 return -EINVAL;
281 if (req->count < 1) {
282 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
283 return -EINVAL;
285 if (req->memory != V4L2_MEMORY_MMAP &&
286 req->memory != V4L2_MEMORY_USERPTR &&
287 req->memory != V4L2_MEMORY_OVERLAY) {
288 dprintk(1,"reqbufs: memory type invalid\n");
289 return -EINVAL;
292 mutex_lock(&q->lock);
293 if (q->streaming) {
294 dprintk(1,"reqbufs: streaming already exists\n");
295 retval = -EBUSY;
296 goto done;
298 if (!list_empty(&q->stream)) {
299 dprintk(1,"reqbufs: stream running\n");
300 retval = -EBUSY;
301 goto done;
304 count = req->count;
305 if (count > VIDEO_MAX_FRAME)
306 count = VIDEO_MAX_FRAME;
307 size = 0;
308 q->ops->buf_setup(q,&count,&size);
309 size = PAGE_ALIGN(size);
310 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
311 count, size, (count*size)>>PAGE_SHIFT);
313 retval = videobuf_mmap_setup(q,count,size,req->memory);
314 if (retval < 0) {
315 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
316 goto done;
319 req->count = count;
321 done:
322 mutex_unlock(&q->lock);
323 return retval;
326 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
328 if (unlikely(b->type != q->type)) {
329 dprintk(1,"querybuf: Wrong type.\n");
330 return -EINVAL;
332 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
333 dprintk(1,"querybuf: index out of range.\n");
334 return -EINVAL;
336 if (unlikely(NULL == q->bufs[b->index])) {
337 dprintk(1,"querybuf: buffer is null.\n");
338 return -EINVAL;
340 videobuf_status(q,b,q->bufs[b->index],q->type);
341 return 0;
344 int videobuf_qbuf(struct videobuf_queue *q,
345 struct v4l2_buffer *b)
347 struct videobuf_buffer *buf;
348 enum v4l2_field field;
349 unsigned long flags=0;
350 int retval;
352 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
354 if (b->memory == V4L2_MEMORY_MMAP)
355 down_read(&current->mm->mmap_sem);
357 mutex_lock(&q->lock);
358 retval = -EBUSY;
359 if (q->reading) {
360 dprintk(1,"qbuf: Reading running...\n");
361 goto done;
363 retval = -EINVAL;
364 if (b->type != q->type) {
365 dprintk(1,"qbuf: Wrong type.\n");
366 goto done;
368 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
369 dprintk(1,"qbuf: index out of range.\n");
370 goto done;
372 buf = q->bufs[b->index];
373 if (NULL == buf) {
374 dprintk(1,"qbuf: buffer is null.\n");
375 goto done;
377 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
378 if (buf->memory != b->memory) {
379 dprintk(1,"qbuf: memory type is wrong.\n");
380 goto done;
382 if (buf->state != STATE_NEEDS_INIT && buf->state != STATE_IDLE) {
383 dprintk(1,"qbuf: buffer is already queued or active.\n");
384 goto done;
387 if (b->flags & V4L2_BUF_FLAG_INPUT) {
388 if (b->input >= q->inputs) {
389 dprintk(1,"qbuf: wrong input.\n");
390 goto done;
392 buf->input = b->input;
393 } else {
394 buf->input = UNSET;
397 switch (b->memory) {
398 case V4L2_MEMORY_MMAP:
399 if (0 == buf->baddr) {
400 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
401 goto done;
403 break;
404 case V4L2_MEMORY_USERPTR:
405 if (b->length < buf->bsize) {
406 dprintk(1,"qbuf: buffer length is not enough\n");
407 goto done;
409 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
410 q->ops->buf_release(q,buf);
411 buf->baddr = b->m.userptr;
412 break;
413 case V4L2_MEMORY_OVERLAY:
414 buf->boff = b->m.offset;
415 break;
416 default:
417 dprintk(1,"qbuf: wrong memory type\n");
418 goto done;
421 dprintk(1,"qbuf: requesting next field\n");
422 field = videobuf_next_field(q);
423 retval = q->ops->buf_prepare(q,buf,field);
424 if (0 != retval) {
425 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
426 goto done;
429 list_add_tail(&buf->stream,&q->stream);
430 if (q->streaming) {
431 if (q->irqlock)
432 spin_lock_irqsave(q->irqlock,flags);
433 q->ops->buf_queue(q,buf);
434 if (q->irqlock)
435 spin_unlock_irqrestore(q->irqlock,flags);
437 dprintk(1,"qbuf: succeded\n");
438 retval = 0;
440 done:
441 mutex_unlock(&q->lock);
443 if (b->memory == V4L2_MEMORY_MMAP)
444 up_read(&current->mm->mmap_sem);
446 return retval;
449 int videobuf_dqbuf(struct videobuf_queue *q,
450 struct v4l2_buffer *b, int nonblocking)
452 struct videobuf_buffer *buf;
453 int retval;
455 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
457 mutex_lock(&q->lock);
458 retval = -EBUSY;
459 if (q->reading) {
460 dprintk(1,"dqbuf: Reading running...\n");
461 goto done;
463 retval = -EINVAL;
464 if (b->type != q->type) {
465 dprintk(1,"dqbuf: Wrong type.\n");
466 goto done;
468 if (list_empty(&q->stream)) {
469 dprintk(1,"dqbuf: stream running\n");
470 goto done;
472 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
473 retval = videobuf_waiton(buf, nonblocking, 1);
474 if (retval < 0) {
475 dprintk(1,"dqbuf: waiton returned %d\n",retval);
476 goto done;
478 switch (buf->state) {
479 case STATE_ERROR:
480 dprintk(1,"dqbuf: state is error\n");
481 retval = -EIO;
482 CALL(q,sync,q, buf);
483 buf->state = STATE_IDLE;
484 break;
485 case STATE_DONE:
486 dprintk(1,"dqbuf: state is done\n");
487 CALL(q,sync,q, buf);
488 buf->state = STATE_IDLE;
489 break;
490 default:
491 dprintk(1,"dqbuf: state invalid\n");
492 retval = -EINVAL;
493 goto done;
495 list_del(&buf->stream);
496 memset(b,0,sizeof(*b));
497 videobuf_status(q,b,buf,q->type);
499 done:
500 mutex_unlock(&q->lock);
501 return retval;
504 int videobuf_streamon(struct videobuf_queue *q)
506 struct videobuf_buffer *buf;
507 struct list_head *list;
508 unsigned long flags=0;
509 int retval;
511 mutex_lock(&q->lock);
512 retval = -EBUSY;
513 if (q->reading)
514 goto done;
515 retval = 0;
516 if (q->streaming)
517 goto done;
518 q->streaming = 1;
519 if (q->irqlock)
520 spin_lock_irqsave(q->irqlock,flags);
521 list_for_each(list,&q->stream) {
522 buf = list_entry(list, struct videobuf_buffer, stream);
523 if (buf->state == STATE_PREPARED)
524 q->ops->buf_queue(q,buf);
526 if (q->irqlock)
527 spin_unlock_irqrestore(q->irqlock,flags);
529 done:
530 mutex_unlock(&q->lock);
531 return retval;
534 int videobuf_streamoff(struct videobuf_queue *q)
536 int retval = -EINVAL;
538 mutex_lock(&q->lock);
539 if (!q->streaming)
540 goto done;
541 videobuf_queue_cancel(q);
542 q->streaming = 0;
543 retval = 0;
545 done:
546 mutex_unlock(&q->lock);
547 return retval;
550 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
551 char __user *data,
552 size_t count, loff_t *ppos)
554 enum v4l2_field field;
555 unsigned long flags=0;
556 int retval;
558 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
560 /* setup stuff */
561 q->read_buf = videobuf_alloc(q);
562 if (NULL == q->read_buf)
563 return -ENOMEM;
565 q->read_buf->memory = V4L2_MEMORY_USERPTR;
566 q->read_buf->baddr = (unsigned long)data;
567 q->read_buf->bsize = count;
569 field = videobuf_next_field(q);
570 retval = q->ops->buf_prepare(q,q->read_buf,field);
571 if (0 != retval)
572 goto done;
574 /* start capture & wait */
575 if (q->irqlock)
576 spin_lock_irqsave(q->irqlock,flags);
577 q->ops->buf_queue(q,q->read_buf);
578 if (q->irqlock)
579 spin_unlock_irqrestore(q->irqlock,flags);
580 retval = videobuf_waiton(q->read_buf,0,0);
581 if (0 == retval) {
582 CALL(q,sync,q,q->read_buf);
583 if (STATE_ERROR == q->read_buf->state)
584 retval = -EIO;
585 else
586 retval = q->read_buf->size;
589 done:
590 /* cleanup */
591 q->ops->buf_release(q,q->read_buf);
592 kfree(q->read_buf);
593 q->read_buf = NULL;
594 return retval;
597 ssize_t videobuf_read_one(struct videobuf_queue *q,
598 char __user *data, size_t count, loff_t *ppos,
599 int nonblocking)
601 enum v4l2_field field;
602 unsigned long flags=0;
603 unsigned size, nbufs;
604 int retval;
606 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
608 mutex_lock(&q->lock);
610 nbufs = 1; size = 0;
611 q->ops->buf_setup(q,&nbufs,&size);
613 if (NULL == q->read_buf &&
614 count >= size &&
615 !nonblocking) {
616 retval = videobuf_read_zerocopy(q,data,count,ppos);
617 if (retval >= 0 || retval == -EIO)
618 /* ok, all done */
619 goto done;
620 /* fallback to kernel bounce buffer on failures */
623 if (NULL == q->read_buf) {
624 /* need to capture a new frame */
625 retval = -ENOMEM;
626 q->read_buf = videobuf_alloc(q);
628 dprintk(1,"video alloc=0x%p\n", q->read_buf);
629 if (NULL == q->read_buf)
630 goto done;
631 q->read_buf->memory = V4L2_MEMORY_USERPTR;
632 q->read_buf->bsize = count; /* preferred size */
633 field = videobuf_next_field(q);
634 retval = q->ops->buf_prepare(q,q->read_buf,field);
636 if (0 != retval) {
637 kfree (q->read_buf);
638 q->read_buf = NULL;
639 goto done;
641 if (q->irqlock)
642 spin_lock_irqsave(q->irqlock,flags);
644 q->ops->buf_queue(q,q->read_buf);
645 if (q->irqlock)
646 spin_unlock_irqrestore(q->irqlock,flags);
647 q->read_off = 0;
650 /* wait until capture is done */
651 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
652 if (0 != retval)
653 goto done;
655 CALL(q,sync,q,q->read_buf);
657 if (STATE_ERROR == q->read_buf->state) {
658 /* catch I/O errors */
659 q->ops->buf_release(q,q->read_buf);
660 kfree(q->read_buf);
661 q->read_buf = NULL;
662 retval = -EIO;
663 goto done;
666 /* Copy to userspace */
667 retval=CALL(q,copy_to_user,q,data,count,nonblocking);
668 if (retval<0)
669 goto done;
671 q->read_off += retval;
672 if (q->read_off == q->read_buf->size) {
673 /* all data copied, cleanup */
674 q->ops->buf_release(q,q->read_buf);
675 kfree(q->read_buf);
676 q->read_buf = NULL;
679 done:
680 mutex_unlock(&q->lock);
681 return retval;
684 int videobuf_read_start(struct videobuf_queue *q)
686 enum v4l2_field field;
687 unsigned long flags=0;
688 int count = 0, size = 0;
689 int err, i;
691 q->ops->buf_setup(q,&count,&size);
692 if (count < 2)
693 count = 2;
694 if (count > VIDEO_MAX_FRAME)
695 count = VIDEO_MAX_FRAME;
696 size = PAGE_ALIGN(size);
698 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
699 if (err)
700 return err;
702 for (i = 0; i < count; i++) {
703 field = videobuf_next_field(q);
704 err = q->ops->buf_prepare(q,q->bufs[i],field);
705 if (err)
706 return err;
707 list_add_tail(&q->bufs[i]->stream, &q->stream);
709 if (q->irqlock)
710 spin_lock_irqsave(q->irqlock,flags);
711 for (i = 0; i < count; i++)
712 q->ops->buf_queue(q,q->bufs[i]);
713 if (q->irqlock)
714 spin_unlock_irqrestore(q->irqlock,flags);
715 q->reading = 1;
716 return 0;
719 void videobuf_read_stop(struct videobuf_queue *q)
721 int i;
723 videobuf_queue_cancel(q);
724 videobuf_mmap_free(q);
725 INIT_LIST_HEAD(&q->stream);
726 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
727 if (NULL == q->bufs[i])
728 continue;
729 kfree(q->bufs[i]);
730 q->bufs[i] = NULL;
732 q->read_buf = NULL;
733 q->reading = 0;
736 ssize_t videobuf_read_stream(struct videobuf_queue *q,
737 char __user *data, size_t count, loff_t *ppos,
738 int vbihack, int nonblocking)
740 int rc, retval;
741 unsigned long flags=0;
743 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
745 dprintk(2,"%s\n",__FUNCTION__);
746 mutex_lock(&q->lock);
747 retval = -EBUSY;
748 if (q->streaming)
749 goto done;
750 if (!q->reading) {
751 retval = videobuf_read_start(q);
752 if (retval < 0)
753 goto done;
756 retval = 0;
757 while (count > 0) {
758 /* get / wait for data */
759 if (NULL == q->read_buf) {
760 q->read_buf = list_entry(q->stream.next,
761 struct videobuf_buffer,
762 stream);
763 list_del(&q->read_buf->stream);
764 q->read_off = 0;
766 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
767 if (rc < 0) {
768 if (0 == retval)
769 retval = rc;
770 break;
773 if (q->read_buf->state == STATE_DONE) {
774 rc = CALL (q,copy_stream, q, data + retval, count,
775 retval, vbihack, nonblocking);
776 if (rc < 0) {
777 retval = rc;
778 break;
780 retval += rc;
781 count -= rc;
782 q->read_off += rc;
783 } else {
784 /* some error */
785 q->read_off = q->read_buf->size;
786 if (0 == retval)
787 retval = -EIO;
790 /* requeue buffer when done with copying */
791 if (q->read_off == q->read_buf->size) {
792 list_add_tail(&q->read_buf->stream,
793 &q->stream);
794 if (q->irqlock)
795 spin_lock_irqsave(q->irqlock,flags);
796 q->ops->buf_queue(q,q->read_buf);
797 if (q->irqlock)
798 spin_unlock_irqrestore(q->irqlock,flags);
799 q->read_buf = NULL;
801 if (retval < 0)
802 break;
805 done:
806 mutex_unlock(&q->lock);
807 return retval;
810 unsigned int videobuf_poll_stream(struct file *file,
811 struct videobuf_queue *q,
812 poll_table *wait)
814 struct videobuf_buffer *buf = NULL;
815 unsigned int rc = 0;
817 mutex_lock(&q->lock);
818 if (q->streaming) {
819 if (!list_empty(&q->stream))
820 buf = list_entry(q->stream.next,
821 struct videobuf_buffer, stream);
822 } else {
823 if (!q->reading)
824 videobuf_read_start(q);
825 if (!q->reading) {
826 rc = POLLERR;
827 } else if (NULL == q->read_buf) {
828 q->read_buf = list_entry(q->stream.next,
829 struct videobuf_buffer,
830 stream);
831 list_del(&q->read_buf->stream);
832 q->read_off = 0;
834 buf = q->read_buf;
836 if (!buf)
837 rc = POLLERR;
839 if (0 == rc) {
840 poll_wait(file, &buf->done, wait);
841 if (buf->state == STATE_DONE ||
842 buf->state == STATE_ERROR)
843 rc = POLLIN|POLLRDNORM;
845 mutex_unlock(&q->lock);
846 return rc;
849 int videobuf_mmap_setup(struct videobuf_queue *q,
850 unsigned int bcount, unsigned int bsize,
851 enum v4l2_memory memory)
853 unsigned int i;
854 int err;
856 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
858 err = videobuf_mmap_free(q);
859 if (0 != err)
860 return err;
862 /* Allocate and initialize buffers */
863 for (i = 0; i < bcount; i++) {
864 q->bufs[i] = videobuf_alloc(q);
866 q->bufs[i]->i = i;
867 q->bufs[i]->input = UNSET;
868 q->bufs[i]->memory = memory;
869 q->bufs[i]->bsize = bsize;
870 switch (memory) {
871 case V4L2_MEMORY_MMAP:
872 q->bufs[i]->boff = bsize * i;
873 break;
874 case V4L2_MEMORY_USERPTR:
875 case V4L2_MEMORY_OVERLAY:
876 /* nothing */
877 break;
881 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
882 bcount,bsize);
884 return 0;
887 int videobuf_mmap_free(struct videobuf_queue *q)
889 int i;
890 int rc;
892 if (!q)
893 return 0;
895 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
897 rc = CALL(q,mmap_free,q);
898 if (rc<0)
899 return rc;
901 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
902 if (NULL == q->bufs[i])
903 continue;
904 q->ops->buf_release(q,q->bufs[i]);
905 kfree(q->bufs[i]);
906 q->bufs[i] = NULL;
909 return rc;
912 int videobuf_mmap_mapper(struct videobuf_queue *q,
913 struct vm_area_struct *vma)
915 int retval;
917 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
919 mutex_lock(&q->lock);
920 retval=CALL(q,mmap_mapper,q,vma);
921 mutex_unlock(&q->lock);
923 return retval;
926 #ifdef CONFIG_VIDEO_V4L1_COMPAT
927 int videobuf_cgmbuf(struct videobuf_queue *q,
928 struct video_mbuf *mbuf, int count)
930 struct v4l2_requestbuffers req;
931 int rc,i;
933 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
935 memset(&req,0,sizeof(req));
936 req.type = q->type;
937 req.count = count;
938 req.memory = V4L2_MEMORY_MMAP;
939 rc = videobuf_reqbufs(q,&req);
940 if (rc < 0)
941 return rc;
943 mbuf->frames = req.count;
944 mbuf->size = 0;
945 for (i = 0; i < mbuf->frames; i++) {
946 mbuf->offsets[i] = q->bufs[i]->boff;
947 mbuf->size += q->bufs[i]->bsize;
950 return 0;
952 #endif
954 /* --------------------------------------------------------------------- */
956 EXPORT_SYMBOL_GPL(videobuf_waiton);
957 EXPORT_SYMBOL_GPL(videobuf_iolock);
959 EXPORT_SYMBOL_GPL(videobuf_alloc);
961 EXPORT_SYMBOL_GPL(videobuf_queue_init);
962 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
963 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
965 EXPORT_SYMBOL_GPL(videobuf_next_field);
966 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
967 EXPORT_SYMBOL_GPL(videobuf_querybuf);
968 EXPORT_SYMBOL_GPL(videobuf_qbuf);
969 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
970 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
971 EXPORT_SYMBOL_GPL(videobuf_streamon);
972 EXPORT_SYMBOL_GPL(videobuf_streamoff);
974 EXPORT_SYMBOL_GPL(videobuf_read_start);
975 EXPORT_SYMBOL_GPL(videobuf_read_stop);
976 EXPORT_SYMBOL_GPL(videobuf_read_stream);
977 EXPORT_SYMBOL_GPL(videobuf_read_one);
978 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
980 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
981 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
982 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
985 * Local variables:
986 * c-basic-offset: 8
987 * End: