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