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