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