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