Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / media / video / videobuf-core.c
blob892c56dc356262643acd2a2c28a20e3622685e4a
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) do { \
26 if (unlikely((is) != (should))) { \
27 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
28 BUG(); } } while (0)
30 static int debug;
31 module_param(debug, int, 0644);
33 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
34 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
35 MODULE_LICENSE("GPL");
37 #define dprintk(level, fmt, arg...) do { \
38 if (debug >= level) \
39 printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
41 /* --------------------------------------------------------------------- */
43 #define CALL(q, f, arg...) \
44 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
46 void *videobuf_alloc(struct videobuf_queue *q)
48 struct videobuf_buffer *vb;
50 BUG_ON(q->msize < sizeof(*vb));
52 if (!q->int_ops || !q->int_ops->alloc) {
53 printk(KERN_ERR "No specific ops defined!\n");
54 BUG();
57 vb = q->int_ops->alloc(q->msize);
59 if (NULL != vb) {
60 init_waitqueue_head(&vb->done);
61 vb->magic = MAGIC_BUFFER;
64 return vb;
67 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
69 int retval = 0;
70 DECLARE_WAITQUEUE(wait, current);
72 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
73 add_wait_queue(&vb->done, &wait);
74 while (vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) {
75 if (non_blocking) {
76 retval = -EAGAIN;
77 break;
79 set_current_state(intr ? TASK_INTERRUPTIBLE
80 : TASK_UNINTERRUPTIBLE);
81 if (vb->state == VIDEOBUF_ACTIVE ||
82 vb->state == VIDEOBUF_QUEUED)
83 schedule();
84 set_current_state(TASK_RUNNING);
85 if (intr && signal_pending(current)) {
86 dprintk(1, "buffer waiton: -EINTR\n");
87 retval = -EINTR;
88 break;
91 remove_wait_queue(&vb->done, &wait);
92 return retval;
95 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
96 struct v4l2_framebuffer *fbuf)
98 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
99 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
101 /* This is required to avoid OOPS on some cases,
102 since mmap_mapper() method should be called before _iolock.
103 On some cases, the mmap_mapper() is called only after scheduling.
105 if (vb->memory == V4L2_MEMORY_MMAP) {
106 wait_event_timeout(vb->done, q->is_mmapped,
107 msecs_to_jiffies(100));
108 if (!q->is_mmapped) {
109 printk(KERN_ERR
110 "Error: mmap_mapper() never called!\n");
111 return -EINVAL;
115 return CALL(q, iolock, q, vb, fbuf);
118 /* --------------------------------------------------------------------- */
121 void videobuf_queue_core_init(struct videobuf_queue *q,
122 struct videobuf_queue_ops *ops,
123 void *dev,
124 spinlock_t *irqlock,
125 enum v4l2_buf_type type,
126 enum v4l2_field field,
127 unsigned int msize,
128 void *priv,
129 struct videobuf_qtype_ops *int_ops)
131 memset(q, 0, sizeof(*q));
132 q->irqlock = irqlock;
133 q->dev = dev;
134 q->type = type;
135 q->field = field;
136 q->msize = msize;
137 q->ops = ops;
138 q->priv_data = priv;
139 q->int_ops = int_ops;
141 /* All buffer operations are mandatory */
142 BUG_ON(!q->ops->buf_setup);
143 BUG_ON(!q->ops->buf_prepare);
144 BUG_ON(!q->ops->buf_queue);
145 BUG_ON(!q->ops->buf_release);
147 /* Having implementations for abstract methods are mandatory */
148 BUG_ON(!q->int_ops);
150 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
151 mutex_init(&q->lock);
152 =======
153 mutex_init(&q->vb_lock);
154 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
155 INIT_LIST_HEAD(&q->stream);
158 /* Locking: Only usage in bttv unsafe find way to remove */
159 int videobuf_queue_is_busy(struct videobuf_queue *q)
161 int i;
163 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
165 if (q->streaming) {
166 dprintk(1, "busy: streaming active\n");
167 return 1;
169 if (q->reading) {
170 dprintk(1, "busy: pending read #1\n");
171 return 1;
173 if (q->read_buf) {
174 dprintk(1, "busy: pending read #2\n");
175 return 1;
177 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
178 if (NULL == q->bufs[i])
179 continue;
180 if (q->bufs[i]->map) {
181 dprintk(1, "busy: buffer #%d mapped\n", i);
182 return 1;
184 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
185 dprintk(1, "busy: buffer #%d queued\n", i);
186 return 1;
188 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
189 dprintk(1, "busy: buffer #%d avtive\n", i);
190 return 1;
193 return 0;
196 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
197 /* Locking: Caller holds q->lock */
198 =======
199 /* Locking: Caller holds q->vb_lock */
200 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
201 void videobuf_queue_cancel(struct videobuf_queue *q)
203 unsigned long flags = 0;
204 int i;
206 /* remove queued buffers from list */
207 if (q->irqlock)
208 spin_lock_irqsave(q->irqlock, flags);
209 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
210 if (NULL == q->bufs[i])
211 continue;
212 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
213 list_del(&q->bufs[i]->queue);
214 q->bufs[i]->state = VIDEOBUF_ERROR;
217 if (q->irqlock)
218 spin_unlock_irqrestore(q->irqlock, flags);
220 /* free all buffers + clear queue */
221 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
222 if (NULL == q->bufs[i])
223 continue;
224 q->ops->buf_release(q, q->bufs[i]);
226 INIT_LIST_HEAD(&q->stream);
229 /* --------------------------------------------------------------------- */
231 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
232 /* Locking: Caller holds q->lock */
233 =======
234 /* Locking: Caller holds q->vb_lock */
235 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
236 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
238 enum v4l2_field field = q->field;
240 BUG_ON(V4L2_FIELD_ANY == field);
242 if (V4L2_FIELD_ALTERNATE == field) {
243 if (V4L2_FIELD_TOP == q->last) {
244 field = V4L2_FIELD_BOTTOM;
245 q->last = V4L2_FIELD_BOTTOM;
246 } else {
247 field = V4L2_FIELD_TOP;
248 q->last = V4L2_FIELD_TOP;
251 return field;
254 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
255 /* Locking: Caller holds q->lock */
256 =======
257 /* Locking: Caller holds q->vb_lock */
258 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
259 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
260 struct videobuf_buffer *vb, enum v4l2_buf_type type)
262 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
263 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
265 b->index = vb->i;
266 b->type = type;
268 b->memory = vb->memory;
269 switch (b->memory) {
270 case V4L2_MEMORY_MMAP:
271 b->m.offset = vb->boff;
272 b->length = vb->bsize;
273 break;
274 case V4L2_MEMORY_USERPTR:
275 b->m.userptr = vb->baddr;
276 b->length = vb->bsize;
277 break;
278 case V4L2_MEMORY_OVERLAY:
279 b->m.offset = vb->boff;
280 break;
283 b->flags = 0;
284 if (vb->map)
285 b->flags |= V4L2_BUF_FLAG_MAPPED;
287 switch (vb->state) {
288 case VIDEOBUF_PREPARED:
289 case VIDEOBUF_QUEUED:
290 case VIDEOBUF_ACTIVE:
291 b->flags |= V4L2_BUF_FLAG_QUEUED;
292 break;
293 case VIDEOBUF_DONE:
294 case VIDEOBUF_ERROR:
295 b->flags |= V4L2_BUF_FLAG_DONE;
296 break;
297 case VIDEOBUF_NEEDS_INIT:
298 case VIDEOBUF_IDLE:
299 /* nothing */
300 break;
303 if (vb->input != UNSET) {
304 b->flags |= V4L2_BUF_FLAG_INPUT;
305 b->input = vb->input;
308 b->field = vb->field;
309 b->timestamp = vb->ts;
310 b->bytesused = vb->size;
311 b->sequence = vb->field_count >> 1;
314 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
315 /* Locking: Caller holds q->lock */
316 =======
317 /* Locking: Caller holds q->vb_lock */
318 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
319 static int __videobuf_mmap_free(struct videobuf_queue *q)
321 int i;
322 int rc;
324 if (!q)
325 return 0;
327 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
330 rc = CALL(q, mmap_free, q);
332 q->is_mmapped = 0;
334 if (rc < 0)
335 return rc;
337 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
338 if (NULL == q->bufs[i])
339 continue;
340 q->ops->buf_release(q, q->bufs[i]);
341 kfree(q->bufs[i]);
342 q->bufs[i] = NULL;
345 return rc;
348 int videobuf_mmap_free(struct videobuf_queue *q)
350 int ret;
351 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
352 mutex_lock(&q->lock);
353 =======
354 mutex_lock(&q->vb_lock);
355 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
356 ret = __videobuf_mmap_free(q);
357 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
358 mutex_unlock(&q->lock);
359 =======
360 mutex_unlock(&q->vb_lock);
361 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
362 return ret;
365 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
366 /* Locking: Caller holds q->lock */
367 =======
368 /* Locking: Caller holds q->vb_lock */
369 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
370 static int __videobuf_mmap_setup(struct videobuf_queue *q,
371 unsigned int bcount, unsigned int bsize,
372 enum v4l2_memory memory)
374 unsigned int i;
375 int err;
377 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
379 err = __videobuf_mmap_free(q);
380 if (0 != err)
381 return err;
383 /* Allocate and initialize buffers */
384 for (i = 0; i < bcount; i++) {
385 q->bufs[i] = videobuf_alloc(q);
387 if (q->bufs[i] == NULL)
388 break;
390 q->bufs[i]->i = i;
391 q->bufs[i]->input = UNSET;
392 q->bufs[i]->memory = memory;
393 q->bufs[i]->bsize = bsize;
394 switch (memory) {
395 case V4L2_MEMORY_MMAP:
396 q->bufs[i]->boff = bsize * i;
397 break;
398 case V4L2_MEMORY_USERPTR:
399 case V4L2_MEMORY_OVERLAY:
400 /* nothing */
401 break;
405 if (!i)
406 return -ENOMEM;
408 dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
409 i, bsize);
411 return i;
414 int videobuf_mmap_setup(struct videobuf_queue *q,
415 unsigned int bcount, unsigned int bsize,
416 enum v4l2_memory memory)
418 int ret;
419 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
420 mutex_lock(&q->lock);
421 =======
422 mutex_lock(&q->vb_lock);
423 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
424 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
425 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
426 mutex_unlock(&q->lock);
427 =======
428 mutex_unlock(&q->vb_lock);
429 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
430 return ret;
433 int videobuf_reqbufs(struct videobuf_queue *q,
434 struct v4l2_requestbuffers *req)
436 unsigned int size, count;
437 int retval;
439 if (req->count < 1) {
440 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
441 return -EINVAL;
444 if (req->memory != V4L2_MEMORY_MMAP &&
445 req->memory != V4L2_MEMORY_USERPTR &&
446 req->memory != V4L2_MEMORY_OVERLAY) {
447 dprintk(1, "reqbufs: memory type invalid\n");
448 return -EINVAL;
451 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
452 mutex_lock(&q->lock);
453 =======
454 mutex_lock(&q->vb_lock);
455 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
456 if (req->type != q->type) {
457 dprintk(1, "reqbufs: queue type invalid\n");
458 retval = -EINVAL;
459 goto done;
462 if (q->streaming) {
463 dprintk(1, "reqbufs: streaming already exists\n");
464 retval = -EBUSY;
465 goto done;
467 if (!list_empty(&q->stream)) {
468 dprintk(1, "reqbufs: stream running\n");
469 retval = -EBUSY;
470 goto done;
473 count = req->count;
474 if (count > VIDEO_MAX_FRAME)
475 count = VIDEO_MAX_FRAME;
476 size = 0;
477 q->ops->buf_setup(q, &count, &size);
478 size = PAGE_ALIGN(size);
479 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
480 count, size, (count*size)>>PAGE_SHIFT);
482 retval = __videobuf_mmap_setup(q, count, size, req->memory);
483 if (retval < 0) {
484 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
485 goto done;
488 req->count = retval;
490 done:
491 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
492 mutex_unlock(&q->lock);
493 =======
494 mutex_unlock(&q->vb_lock);
495 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
496 return retval;
499 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
501 int ret = -EINVAL;
503 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
504 mutex_lock(&q->lock);
505 =======
506 mutex_lock(&q->vb_lock);
507 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
508 if (unlikely(b->type != q->type)) {
509 dprintk(1, "querybuf: Wrong type.\n");
510 goto done;
512 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
513 dprintk(1, "querybuf: index out of range.\n");
514 goto done;
516 if (unlikely(NULL == q->bufs[b->index])) {
517 dprintk(1, "querybuf: buffer is null.\n");
518 goto done;
521 videobuf_status(q, b, q->bufs[b->index], q->type);
523 ret = 0;
524 done:
525 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
526 mutex_unlock(&q->lock);
527 =======
528 mutex_unlock(&q->vb_lock);
529 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
530 return ret;
533 int videobuf_qbuf(struct videobuf_queue *q,
534 struct v4l2_buffer *b)
536 struct videobuf_buffer *buf;
537 enum v4l2_field field;
538 unsigned long flags = 0;
539 int retval;
541 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
543 if (b->memory == V4L2_MEMORY_MMAP)
544 down_read(&current->mm->mmap_sem);
546 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
547 mutex_lock(&q->lock);
548 =======
549 mutex_lock(&q->vb_lock);
550 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
551 retval = -EBUSY;
552 if (q->reading) {
553 dprintk(1, "qbuf: Reading running...\n");
554 goto done;
556 retval = -EINVAL;
557 if (b->type != q->type) {
558 dprintk(1, "qbuf: Wrong type.\n");
559 goto done;
561 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
562 dprintk(1, "qbuf: index out of range.\n");
563 goto done;
565 buf = q->bufs[b->index];
566 if (NULL == buf) {
567 dprintk(1, "qbuf: buffer is null.\n");
568 goto done;
570 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
571 if (buf->memory != b->memory) {
572 dprintk(1, "qbuf: memory type is wrong.\n");
573 goto done;
575 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
576 dprintk(1, "qbuf: buffer is already queued or active.\n");
577 goto done;
580 if (b->flags & V4L2_BUF_FLAG_INPUT) {
581 if (b->input >= q->inputs) {
582 dprintk(1, "qbuf: wrong input.\n");
583 goto done;
585 buf->input = b->input;
586 } else {
587 buf->input = UNSET;
590 switch (b->memory) {
591 case V4L2_MEMORY_MMAP:
592 if (0 == buf->baddr) {
593 dprintk(1, "qbuf: mmap requested "
594 "but buffer addr is zero!\n");
595 goto done;
597 break;
598 case V4L2_MEMORY_USERPTR:
599 if (b->length < buf->bsize) {
600 dprintk(1, "qbuf: buffer length is not enough\n");
601 goto done;
603 if (VIDEOBUF_NEEDS_INIT != buf->state &&
604 buf->baddr != b->m.userptr)
605 q->ops->buf_release(q, buf);
606 buf->baddr = b->m.userptr;
607 break;
608 case V4L2_MEMORY_OVERLAY:
609 buf->boff = b->m.offset;
610 break;
611 default:
612 dprintk(1, "qbuf: wrong memory type\n");
613 goto done;
616 dprintk(1, "qbuf: requesting next field\n");
617 field = videobuf_next_field(q);
618 retval = q->ops->buf_prepare(q, buf, field);
619 if (0 != retval) {
620 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
621 goto done;
624 list_add_tail(&buf->stream, &q->stream);
625 if (q->streaming) {
626 if (q->irqlock)
627 spin_lock_irqsave(q->irqlock, flags);
628 q->ops->buf_queue(q, buf);
629 if (q->irqlock)
630 spin_unlock_irqrestore(q->irqlock, flags);
632 dprintk(1, "qbuf: succeded\n");
633 retval = 0;
635 done:
636 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
637 mutex_unlock(&q->lock);
638 =======
639 mutex_unlock(&q->vb_lock);
640 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
642 if (b->memory == V4L2_MEMORY_MMAP)
643 up_read(&current->mm->mmap_sem);
645 return retval;
648 int videobuf_dqbuf(struct videobuf_queue *q,
649 struct v4l2_buffer *b, int nonblocking)
651 struct videobuf_buffer *buf;
652 int retval;
654 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
656 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
657 mutex_lock(&q->lock);
658 =======
659 mutex_lock(&q->vb_lock);
660 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
661 retval = -EBUSY;
662 if (q->reading) {
663 dprintk(1, "dqbuf: Reading running...\n");
664 goto done;
666 retval = -EINVAL;
667 if (b->type != q->type) {
668 dprintk(1, "dqbuf: Wrong type.\n");
669 goto done;
671 if (list_empty(&q->stream)) {
672 dprintk(1, "dqbuf: stream running\n");
673 goto done;
675 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
676 retval = videobuf_waiton(buf, nonblocking, 1);
677 if (retval < 0) {
678 dprintk(1, "dqbuf: waiton returned %d\n", retval);
679 goto done;
681 switch (buf->state) {
682 case VIDEOBUF_ERROR:
683 dprintk(1, "dqbuf: state is error\n");
684 retval = -EIO;
685 CALL(q, sync, q, buf);
686 buf->state = VIDEOBUF_IDLE;
687 break;
688 case VIDEOBUF_DONE:
689 dprintk(1, "dqbuf: state is done\n");
690 CALL(q, sync, q, buf);
691 buf->state = VIDEOBUF_IDLE;
692 break;
693 default:
694 dprintk(1, "dqbuf: state invalid\n");
695 retval = -EINVAL;
696 goto done;
698 list_del(&buf->stream);
699 memset(b, 0, sizeof(*b));
700 videobuf_status(q, b, buf, q->type);
702 done:
703 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
704 mutex_unlock(&q->lock);
705 =======
706 mutex_unlock(&q->vb_lock);
707 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
708 return retval;
711 int videobuf_streamon(struct videobuf_queue *q)
713 struct videobuf_buffer *buf;
714 unsigned long flags = 0;
715 int retval;
717 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
718 mutex_lock(&q->lock);
719 =======
720 mutex_lock(&q->vb_lock);
721 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
722 retval = -EBUSY;
723 if (q->reading)
724 goto done;
725 retval = 0;
726 if (q->streaming)
727 goto done;
728 q->streaming = 1;
729 if (q->irqlock)
730 spin_lock_irqsave(q->irqlock, flags);
731 list_for_each_entry(buf, &q->stream, stream)
732 if (buf->state == VIDEOBUF_PREPARED)
733 q->ops->buf_queue(q, buf);
734 if (q->irqlock)
735 spin_unlock_irqrestore(q->irqlock, flags);
737 done:
738 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
739 mutex_unlock(&q->lock);
740 =======
741 mutex_unlock(&q->vb_lock);
742 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
743 return retval;
746 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
747 /* Locking: Caller holds q->lock */
748 =======
749 /* Locking: Caller holds q->vb_lock */
750 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
751 static int __videobuf_streamoff(struct videobuf_queue *q)
753 if (!q->streaming)
754 return -EINVAL;
756 videobuf_queue_cancel(q);
757 q->streaming = 0;
759 return 0;
762 int videobuf_streamoff(struct videobuf_queue *q)
764 int retval;
766 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
767 mutex_lock(&q->lock);
768 =======
769 mutex_lock(&q->vb_lock);
770 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
771 retval = __videobuf_streamoff(q);
772 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
773 mutex_unlock(&q->lock);
774 =======
775 mutex_unlock(&q->vb_lock);
776 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
778 return retval;
781 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
782 /* Locking: Caller holds q->lock */
783 =======
784 /* Locking: Caller holds q->vb_lock */
785 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
786 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
787 char __user *data,
788 size_t count, loff_t *ppos)
790 enum v4l2_field field;
791 unsigned long flags = 0;
792 int retval;
794 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
796 /* setup stuff */
797 q->read_buf = videobuf_alloc(q);
798 if (NULL == q->read_buf)
799 return -ENOMEM;
801 q->read_buf->memory = V4L2_MEMORY_USERPTR;
802 q->read_buf->baddr = (unsigned long)data;
803 q->read_buf->bsize = count;
805 field = videobuf_next_field(q);
806 retval = q->ops->buf_prepare(q, q->read_buf, field);
807 if (0 != retval)
808 goto done;
810 /* start capture & wait */
811 if (q->irqlock)
812 spin_lock_irqsave(q->irqlock, flags);
813 q->ops->buf_queue(q, q->read_buf);
814 if (q->irqlock)
815 spin_unlock_irqrestore(q->irqlock, flags);
816 retval = videobuf_waiton(q->read_buf, 0, 0);
817 if (0 == retval) {
818 CALL(q, sync, q, q->read_buf);
819 if (VIDEOBUF_ERROR == q->read_buf->state)
820 retval = -EIO;
821 else
822 retval = q->read_buf->size;
825 done:
826 /* cleanup */
827 q->ops->buf_release(q, q->read_buf);
828 kfree(q->read_buf);
829 q->read_buf = NULL;
830 return retval;
833 ssize_t videobuf_read_one(struct videobuf_queue *q,
834 char __user *data, size_t count, loff_t *ppos,
835 int nonblocking)
837 enum v4l2_field field;
838 unsigned long flags = 0;
839 unsigned size, nbufs;
840 int retval;
842 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
844 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
845 mutex_lock(&q->lock);
846 =======
847 mutex_lock(&q->vb_lock);
848 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
850 nbufs = 1; size = 0;
851 q->ops->buf_setup(q, &nbufs, &size);
853 if (NULL == q->read_buf &&
854 count >= size &&
855 !nonblocking) {
856 retval = videobuf_read_zerocopy(q, data, count, ppos);
857 if (retval >= 0 || retval == -EIO)
858 /* ok, all done */
859 goto done;
860 /* fallback to kernel bounce buffer on failures */
863 if (NULL == q->read_buf) {
864 /* need to capture a new frame */
865 retval = -ENOMEM;
866 q->read_buf = videobuf_alloc(q);
868 dprintk(1, "video alloc=0x%p\n", q->read_buf);
869 if (NULL == q->read_buf)
870 goto done;
871 q->read_buf->memory = V4L2_MEMORY_USERPTR;
872 q->read_buf->bsize = count; /* preferred size */
873 field = videobuf_next_field(q);
874 retval = q->ops->buf_prepare(q, q->read_buf, field);
876 if (0 != retval) {
877 kfree(q->read_buf);
878 q->read_buf = NULL;
879 goto done;
881 if (q->irqlock)
882 spin_lock_irqsave(q->irqlock, flags);
884 q->ops->buf_queue(q, q->read_buf);
885 if (q->irqlock)
886 spin_unlock_irqrestore(q->irqlock, flags);
887 q->read_off = 0;
890 /* wait until capture is done */
891 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
892 if (0 != retval)
893 goto done;
895 CALL(q, sync, q, q->read_buf);
897 if (VIDEOBUF_ERROR == q->read_buf->state) {
898 /* catch I/O errors */
899 q->ops->buf_release(q, q->read_buf);
900 kfree(q->read_buf);
901 q->read_buf = NULL;
902 retval = -EIO;
903 goto done;
906 /* Copy to userspace */
907 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
908 if (retval < 0)
909 goto done;
911 q->read_off += retval;
912 if (q->read_off == q->read_buf->size) {
913 /* all data copied, cleanup */
914 q->ops->buf_release(q, q->read_buf);
915 kfree(q->read_buf);
916 q->read_buf = NULL;
919 done:
920 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
921 mutex_unlock(&q->lock);
922 =======
923 mutex_unlock(&q->vb_lock);
924 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
925 return retval;
928 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
929 /* Locking: Caller holds q->lock */
930 =======
931 /* Locking: Caller holds q->vb_lock */
932 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
933 static int __videobuf_read_start(struct videobuf_queue *q)
935 enum v4l2_field field;
936 unsigned long flags = 0;
937 unsigned int count = 0, size = 0;
938 int err, i;
940 q->ops->buf_setup(q, &count, &size);
941 if (count < 2)
942 count = 2;
943 if (count > VIDEO_MAX_FRAME)
944 count = VIDEO_MAX_FRAME;
945 size = PAGE_ALIGN(size);
947 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
948 if (err < 0)
949 return err;
951 count = err;
953 for (i = 0; i < count; i++) {
954 field = videobuf_next_field(q);
955 err = q->ops->buf_prepare(q, q->bufs[i], field);
956 if (err)
957 return err;
958 list_add_tail(&q->bufs[i]->stream, &q->stream);
960 if (q->irqlock)
961 spin_lock_irqsave(q->irqlock, flags);
962 for (i = 0; i < count; i++)
963 q->ops->buf_queue(q, q->bufs[i]);
964 if (q->irqlock)
965 spin_unlock_irqrestore(q->irqlock, flags);
966 q->reading = 1;
967 return 0;
970 static void __videobuf_read_stop(struct videobuf_queue *q)
972 int i;
975 videobuf_queue_cancel(q);
976 __videobuf_mmap_free(q);
977 INIT_LIST_HEAD(&q->stream);
978 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
979 if (NULL == q->bufs[i])
980 continue;
981 kfree(q->bufs[i]);
982 q->bufs[i] = NULL;
984 q->read_buf = NULL;
985 q->reading = 0;
989 int videobuf_read_start(struct videobuf_queue *q)
991 int rc;
993 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
994 mutex_lock(&q->lock);
995 =======
996 mutex_lock(&q->vb_lock);
997 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
998 rc = __videobuf_read_start(q);
999 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1000 mutex_unlock(&q->lock);
1001 =======
1002 mutex_unlock(&q->vb_lock);
1003 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1005 return rc;
1008 void videobuf_read_stop(struct videobuf_queue *q)
1010 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1011 mutex_lock(&q->lock);
1012 =======
1013 mutex_lock(&q->vb_lock);
1014 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1015 __videobuf_read_stop(q);
1016 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1017 mutex_unlock(&q->lock);
1018 =======
1019 mutex_unlock(&q->vb_lock);
1020 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1023 void videobuf_stop(struct videobuf_queue *q)
1025 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1026 mutex_lock(&q->lock);
1027 =======
1028 mutex_lock(&q->vb_lock);
1029 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1031 if (q->streaming)
1032 __videobuf_streamoff(q);
1034 if (q->reading)
1035 __videobuf_read_stop(q);
1037 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1038 mutex_unlock(&q->lock);
1039 =======
1040 mutex_unlock(&q->vb_lock);
1041 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1045 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1046 char __user *data, size_t count, loff_t *ppos,
1047 int vbihack, int nonblocking)
1049 int rc, retval;
1050 unsigned long flags = 0;
1052 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1054 dprintk(2, "%s\n", __FUNCTION__);
1055 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1056 mutex_lock(&q->lock);
1057 =======
1058 mutex_lock(&q->vb_lock);
1059 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1060 retval = -EBUSY;
1061 if (q->streaming)
1062 goto done;
1063 if (!q->reading) {
1064 retval = __videobuf_read_start(q);
1065 if (retval < 0)
1066 goto done;
1069 retval = 0;
1070 while (count > 0) {
1071 /* get / wait for data */
1072 if (NULL == q->read_buf) {
1073 q->read_buf = list_entry(q->stream.next,
1074 struct videobuf_buffer,
1075 stream);
1076 list_del(&q->read_buf->stream);
1077 q->read_off = 0;
1079 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1080 if (rc < 0) {
1081 if (0 == retval)
1082 retval = rc;
1083 break;
1086 if (q->read_buf->state == VIDEOBUF_DONE) {
1087 rc = CALL(q, copy_stream, q, data + retval, count,
1088 retval, vbihack, nonblocking);
1089 if (rc < 0) {
1090 retval = rc;
1091 break;
1093 retval += rc;
1094 count -= rc;
1095 q->read_off += rc;
1096 } else {
1097 /* some error */
1098 q->read_off = q->read_buf->size;
1099 if (0 == retval)
1100 retval = -EIO;
1103 /* requeue buffer when done with copying */
1104 if (q->read_off == q->read_buf->size) {
1105 list_add_tail(&q->read_buf->stream,
1106 &q->stream);
1107 if (q->irqlock)
1108 spin_lock_irqsave(q->irqlock, flags);
1109 q->ops->buf_queue(q, q->read_buf);
1110 if (q->irqlock)
1111 spin_unlock_irqrestore(q->irqlock, flags);
1112 q->read_buf = NULL;
1114 if (retval < 0)
1115 break;
1118 done:
1119 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1120 mutex_unlock(&q->lock);
1121 =======
1122 mutex_unlock(&q->vb_lock);
1123 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1124 return retval;
1127 unsigned int videobuf_poll_stream(struct file *file,
1128 struct videobuf_queue *q,
1129 poll_table *wait)
1131 struct videobuf_buffer *buf = NULL;
1132 unsigned int rc = 0;
1134 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1135 mutex_lock(&q->lock);
1136 =======
1137 mutex_lock(&q->vb_lock);
1138 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1139 if (q->streaming) {
1140 if (!list_empty(&q->stream))
1141 buf = list_entry(q->stream.next,
1142 struct videobuf_buffer, stream);
1143 } else {
1144 if (!q->reading)
1145 __videobuf_read_start(q);
1146 if (!q->reading) {
1147 rc = POLLERR;
1148 } else if (NULL == q->read_buf) {
1149 q->read_buf = list_entry(q->stream.next,
1150 struct videobuf_buffer,
1151 stream);
1152 list_del(&q->read_buf->stream);
1153 q->read_off = 0;
1155 buf = q->read_buf;
1157 if (!buf)
1158 rc = POLLERR;
1160 if (0 == rc) {
1161 poll_wait(file, &buf->done, wait);
1162 if (buf->state == VIDEOBUF_DONE ||
1163 buf->state == VIDEOBUF_ERROR)
1164 rc = POLLIN|POLLRDNORM;
1166 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1167 mutex_unlock(&q->lock);
1168 =======
1169 mutex_unlock(&q->vb_lock);
1170 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1171 return rc;
1174 int videobuf_mmap_mapper(struct videobuf_queue *q,
1175 struct vm_area_struct *vma)
1177 int retval;
1179 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1181 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1182 mutex_lock(&q->lock);
1183 =======
1184 mutex_lock(&q->vb_lock);
1185 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1186 retval = CALL(q, mmap_mapper, q, vma);
1187 q->is_mmapped = 1;
1188 <<<<<<< HEAD:drivers/media/video/videobuf-core.c
1189 mutex_unlock(&q->lock);
1190 =======
1191 mutex_unlock(&q->vb_lock);
1192 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/videobuf-core.c
1194 return retval;
1197 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1198 int videobuf_cgmbuf(struct videobuf_queue *q,
1199 struct video_mbuf *mbuf, int count)
1201 struct v4l2_requestbuffers req;
1202 int rc, i;
1204 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1206 memset(&req, 0, sizeof(req));
1207 req.type = q->type;
1208 req.count = count;
1209 req.memory = V4L2_MEMORY_MMAP;
1210 rc = videobuf_reqbufs(q, &req);
1211 if (rc < 0)
1212 return rc;
1214 mbuf->frames = req.count;
1215 mbuf->size = 0;
1216 for (i = 0; i < mbuf->frames; i++) {
1217 mbuf->offsets[i] = q->bufs[i]->boff;
1218 mbuf->size += q->bufs[i]->bsize;
1221 return 0;
1223 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1224 #endif
1226 /* --------------------------------------------------------------------- */
1228 EXPORT_SYMBOL_GPL(videobuf_waiton);
1229 EXPORT_SYMBOL_GPL(videobuf_iolock);
1231 EXPORT_SYMBOL_GPL(videobuf_alloc);
1233 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
1234 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1235 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1237 EXPORT_SYMBOL_GPL(videobuf_next_field);
1238 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1239 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1240 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1241 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1242 EXPORT_SYMBOL_GPL(videobuf_streamon);
1243 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1245 EXPORT_SYMBOL_GPL(videobuf_read_start);
1246 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1247 EXPORT_SYMBOL_GPL(videobuf_stop);
1248 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1249 EXPORT_SYMBOL_GPL(videobuf_read_one);
1250 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1252 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1253 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1254 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);