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