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