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