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