V4L/DVB (8125): This driver adds support for the Sensoray 2255 devices.
[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         if (q->int_ops->vmalloc)
101                 return q->int_ops->vmalloc(buf);
102         else
103                 return NULL;
104 }
105 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
106
107 /* --------------------------------------------------------------------- */
108
109
110 void videobuf_queue_core_init(struct videobuf_queue *q,
111                          struct videobuf_queue_ops *ops,
112                          struct device *dev,
113                          spinlock_t *irqlock,
114                          enum v4l2_buf_type type,
115                          enum v4l2_field field,
116                          unsigned int msize,
117                          void *priv,
118                          struct videobuf_qtype_ops *int_ops)
119 {
120         memset(q, 0, sizeof(*q));
121         q->irqlock   = irqlock;
122         q->dev       = dev;
123         q->type      = type;
124         q->field     = field;
125         q->msize     = msize;
126         q->ops       = ops;
127         q->priv_data = priv;
128         q->int_ops   = int_ops;
129
130         /* All buffer operations are mandatory */
131         BUG_ON(!q->ops->buf_setup);
132         BUG_ON(!q->ops->buf_prepare);
133         BUG_ON(!q->ops->buf_queue);
134         BUG_ON(!q->ops->buf_release);
135
136         /* Lock is mandatory for queue_cancel to work */
137         BUG_ON(!irqlock);
138
139         /* Having implementations for abstract methods are mandatory */
140         BUG_ON(!q->int_ops);
141
142         mutex_init(&q->vb_lock);
143         init_waitqueue_head(&q->wait);
144         INIT_LIST_HEAD(&q->stream);
145 }
146
147 /* Locking: Only usage in bttv unsafe find way to remove */
148 int videobuf_queue_is_busy(struct videobuf_queue *q)
149 {
150         int i;
151
152         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
153
154         if (q->streaming) {
155                 dprintk(1, "busy: streaming active\n");
156                 return 1;
157         }
158         if (q->reading) {
159                 dprintk(1, "busy: pending read #1\n");
160                 return 1;
161         }
162         if (q->read_buf) {
163                 dprintk(1, "busy: pending read #2\n");
164                 return 1;
165         }
166         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
167                 if (NULL == q->bufs[i])
168                         continue;
169                 if (q->bufs[i]->map) {
170                         dprintk(1, "busy: buffer #%d mapped\n", i);
171                         return 1;
172                 }
173                 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
174                         dprintk(1, "busy: buffer #%d queued\n", i);
175                         return 1;
176                 }
177                 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
178                         dprintk(1, "busy: buffer #%d avtive\n", i);
179                         return 1;
180                 }
181         }
182         return 0;
183 }
184
185 /* Locking: Caller holds q->vb_lock */
186 void videobuf_queue_cancel(struct videobuf_queue *q)
187 {
188         unsigned long flags = 0;
189         int i;
190
191         q->streaming = 0;
192         q->reading  = 0;
193         wake_up_interruptible_sync(&q->wait);
194
195         /* remove queued buffers from list */
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                         wake_up_all(&q->bufs[i]->done);
204                 }
205         }
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->vb_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->vb_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->vb_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->vb_lock);
328         ret = __videobuf_mmap_free(q);
329         mutex_unlock(&q->vb_lock);
330         return ret;
331 }
332
333 /* Locking: Caller holds q->vb_lock */
334 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->vb_lock);
384         ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
385         mutex_unlock(&q->vb_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->vb_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->vb_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->vb_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->vb_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->vb_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                 spin_lock_irqsave(q->irqlock, flags);
563                 q->ops->buf_queue(q, buf);
564                 spin_unlock_irqrestore(q->irqlock, flags);
565         }
566         dprintk(1, "qbuf: succeded\n");
567         retval = 0;
568         wake_up_interruptible_sync(&q->wait);
569
570  done:
571         mutex_unlock(&q->vb_lock);
572
573         if (b->memory == V4L2_MEMORY_MMAP)
574                 up_read(&current->mm->mmap_sem);
575
576         return retval;
577 }
578
579
580 /* Locking: Caller holds q->vb_lock */
581 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
582 {
583         int retval;
584
585 checks:
586         if (!q->streaming) {
587                 dprintk(1, "next_buffer: Not streaming\n");
588                 retval = -EINVAL;
589                 goto done;
590         }
591
592         if (list_empty(&q->stream)) {
593                 if (noblock) {
594                         retval = -EAGAIN;
595                         dprintk(2, "next_buffer: no buffers to dequeue\n");
596                         goto done;
597                 } else {
598                         dprintk(2, "next_buffer: waiting on buffer\n");
599
600                         /* Drop lock to avoid deadlock with qbuf */
601                         mutex_unlock(&q->vb_lock);
602
603                         /* Checking list_empty and streaming is safe without
604                          * locks because we goto checks to validate while
605                          * holding locks before proceeding */
606                         retval = wait_event_interruptible(q->wait,
607                                 !list_empty(&q->stream) || !q->streaming);
608                         mutex_lock(&q->vb_lock);
609
610                         if (retval)
611                                 goto done;
612
613                         goto checks;
614                 }
615         }
616
617         retval = 0;
618
619 done:
620         return retval;
621 }
622
623
624 /* Locking: Caller holds q->vb_lock */
625 static int stream_next_buffer(struct videobuf_queue *q,
626                         struct videobuf_buffer **vb, int nonblocking)
627 {
628         int retval;
629         struct videobuf_buffer *buf = NULL;
630
631         retval = stream_next_buffer_check_queue(q, nonblocking);
632         if (retval)
633                 goto done;
634
635         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
636         retval = videobuf_waiton(buf, nonblocking, 1);
637         if (retval < 0)
638                 goto done;
639
640         *vb = buf;
641 done:
642         return retval;
643 }
644
645 int videobuf_dqbuf(struct videobuf_queue *q,
646                struct v4l2_buffer *b, int nonblocking)
647 {
648         struct videobuf_buffer *buf = NULL;
649         int retval;
650
651         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
652
653         mutex_lock(&q->vb_lock);
654
655         retval = stream_next_buffer(q, &buf, nonblocking);
656         if (retval < 0) {
657                 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
658                 goto done;
659         }
660
661         switch (buf->state) {
662         case VIDEOBUF_ERROR:
663                 dprintk(1, "dqbuf: state is error\n");
664                 retval = -EIO;
665                 CALL(q, sync, q, buf);
666                 buf->state = VIDEOBUF_IDLE;
667                 break;
668         case VIDEOBUF_DONE:
669                 dprintk(1, "dqbuf: state is done\n");
670                 CALL(q, sync, q, buf);
671                 buf->state = VIDEOBUF_IDLE;
672                 break;
673         default:
674                 dprintk(1, "dqbuf: state invalid\n");
675                 retval = -EINVAL;
676                 goto done;
677         }
678         list_del(&buf->stream);
679         memset(b, 0, sizeof(*b));
680         videobuf_status(q, b, buf, q->type);
681
682  done:
683         mutex_unlock(&q->vb_lock);
684         return retval;
685 }
686
687 int videobuf_streamon(struct videobuf_queue *q)
688 {
689         struct videobuf_buffer *buf;
690         unsigned long flags = 0;
691         int retval;
692
693         mutex_lock(&q->vb_lock);
694         retval = -EBUSY;
695         if (q->reading)
696                 goto done;
697         retval = 0;
698         if (q->streaming)
699                 goto done;
700         q->streaming = 1;
701         spin_lock_irqsave(q->irqlock, flags);
702         list_for_each_entry(buf, &q->stream, stream)
703                 if (buf->state == VIDEOBUF_PREPARED)
704                         q->ops->buf_queue(q, buf);
705         spin_unlock_irqrestore(q->irqlock, flags);
706
707         wake_up_interruptible_sync(&q->wait);
708  done:
709         mutex_unlock(&q->vb_lock);
710         return retval;
711 }
712
713 /* Locking: Caller holds q->vb_lock */
714 static int __videobuf_streamoff(struct videobuf_queue *q)
715 {
716         if (!q->streaming)
717                 return -EINVAL;
718
719         videobuf_queue_cancel(q);
720
721         return 0;
722 }
723
724 int videobuf_streamoff(struct videobuf_queue *q)
725 {
726         int retval;
727
728         mutex_lock(&q->vb_lock);
729         retval = __videobuf_streamoff(q);
730         mutex_unlock(&q->vb_lock);
731
732         return retval;
733 }
734
735 /* Locking: Caller holds q->vb_lock */
736 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
737                                       char __user *data,
738                                       size_t count, loff_t *ppos)
739 {
740         enum v4l2_field field;
741         unsigned long flags = 0;
742         int retval;
743
744         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
745
746         /* setup stuff */
747         q->read_buf = videobuf_alloc(q);
748         if (NULL == q->read_buf)
749                 return -ENOMEM;
750
751         q->read_buf->memory = V4L2_MEMORY_USERPTR;
752         q->read_buf->baddr  = (unsigned long)data;
753         q->read_buf->bsize  = count;
754
755         field = videobuf_next_field(q);
756         retval = q->ops->buf_prepare(q, q->read_buf, field);
757         if (0 != retval)
758                 goto done;
759
760         /* start capture & wait */
761         spin_lock_irqsave(q->irqlock, flags);
762         q->ops->buf_queue(q, q->read_buf);
763         spin_unlock_irqrestore(q->irqlock, flags);
764         retval = videobuf_waiton(q->read_buf, 0, 0);
765         if (0 == retval) {
766                 CALL(q, sync, q, q->read_buf);
767                 if (VIDEOBUF_ERROR == q->read_buf->state)
768                         retval = -EIO;
769                 else
770                         retval = q->read_buf->size;
771         }
772
773  done:
774         /* cleanup */
775         q->ops->buf_release(q, q->read_buf);
776         kfree(q->read_buf);
777         q->read_buf = NULL;
778         return retval;
779 }
780
781 ssize_t videobuf_read_one(struct videobuf_queue *q,
782                           char __user *data, size_t count, loff_t *ppos,
783                           int nonblocking)
784 {
785         enum v4l2_field field;
786         unsigned long flags = 0;
787         unsigned size = 0, nbufs = 1;
788         int retval;
789
790         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
791
792         mutex_lock(&q->vb_lock);
793
794         q->ops->buf_setup(q, &nbufs, &size);
795
796         if (NULL == q->read_buf  &&
797             count >= size        &&
798             !nonblocking) {
799                 retval = videobuf_read_zerocopy(q, data, count, ppos);
800                 if (retval >= 0  ||  retval == -EIO)
801                         /* ok, all done */
802                         goto done;
803                 /* fallback to kernel bounce buffer on failures */
804         }
805
806         if (NULL == q->read_buf) {
807                 /* need to capture a new frame */
808                 retval = -ENOMEM;
809                 q->read_buf = videobuf_alloc(q);
810
811                 dprintk(1, "video alloc=0x%p\n", q->read_buf);
812                 if (NULL == q->read_buf)
813                         goto done;
814                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
815                 q->read_buf->bsize = count; /* preferred size */
816                 field = videobuf_next_field(q);
817                 retval = q->ops->buf_prepare(q, q->read_buf, field);
818
819                 if (0 != retval) {
820                         kfree(q->read_buf);
821                         q->read_buf = NULL;
822                         goto done;
823                 }
824
825                 spin_lock_irqsave(q->irqlock, flags);
826                 q->ops->buf_queue(q, q->read_buf);
827                 spin_unlock_irqrestore(q->irqlock, flags);
828
829                 q->read_off = 0;
830         }
831
832         /* wait until capture is done */
833         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
834         if (0 != retval)
835                 goto done;
836
837         CALL(q, sync, q, q->read_buf);
838
839         if (VIDEOBUF_ERROR == q->read_buf->state) {
840                 /* catch I/O errors */
841                 q->ops->buf_release(q, q->read_buf);
842                 kfree(q->read_buf);
843                 q->read_buf = NULL;
844                 retval = -EIO;
845                 goto done;
846         }
847
848         /* Copy to userspace */
849         retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
850         if (retval < 0)
851                 goto done;
852
853         q->read_off += retval;
854         if (q->read_off == q->read_buf->size) {
855                 /* all data copied, cleanup */
856                 q->ops->buf_release(q, q->read_buf);
857                 kfree(q->read_buf);
858                 q->read_buf = NULL;
859         }
860
861  done:
862         mutex_unlock(&q->vb_lock);
863         return retval;
864 }
865
866 /* Locking: Caller holds q->vb_lock */
867 static int __videobuf_read_start(struct videobuf_queue *q)
868 {
869         enum v4l2_field field;
870         unsigned long flags = 0;
871         unsigned int count = 0, size = 0;
872         int err, i;
873
874         q->ops->buf_setup(q, &count, &size);
875         if (count < 2)
876                 count = 2;
877         if (count > VIDEO_MAX_FRAME)
878                 count = VIDEO_MAX_FRAME;
879         size = PAGE_ALIGN(size);
880
881         err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
882         if (err < 0)
883                 return err;
884
885         count = err;
886
887         for (i = 0; i < count; i++) {
888                 field = videobuf_next_field(q);
889                 err = q->ops->buf_prepare(q, q->bufs[i], field);
890                 if (err)
891                         return err;
892                 list_add_tail(&q->bufs[i]->stream, &q->stream);
893         }
894         spin_lock_irqsave(q->irqlock, flags);
895         for (i = 0; i < count; i++)
896                 q->ops->buf_queue(q, q->bufs[i]);
897         spin_unlock_irqrestore(q->irqlock, flags);
898         q->reading = 1;
899         return 0;
900 }
901
902 static void __videobuf_read_stop(struct videobuf_queue *q)
903 {
904         int i;
905
906         videobuf_queue_cancel(q);
907         __videobuf_mmap_free(q);
908         INIT_LIST_HEAD(&q->stream);
909         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
910                 if (NULL == q->bufs[i])
911                         continue;
912                 kfree(q->bufs[i]);
913                 q->bufs[i] = NULL;
914         }
915         q->read_buf = NULL;
916
917 }
918
919 int videobuf_read_start(struct videobuf_queue *q)
920 {
921         int rc;
922
923         mutex_lock(&q->vb_lock);
924         rc = __videobuf_read_start(q);
925         mutex_unlock(&q->vb_lock);
926
927         return rc;
928 }
929
930 void videobuf_read_stop(struct videobuf_queue *q)
931 {
932         mutex_lock(&q->vb_lock);
933         __videobuf_read_stop(q);
934         mutex_unlock(&q->vb_lock);
935 }
936
937 void videobuf_stop(struct videobuf_queue *q)
938 {
939         mutex_lock(&q->vb_lock);
940
941         if (q->streaming)
942                 __videobuf_streamoff(q);
943
944         if (q->reading)
945                 __videobuf_read_stop(q);
946
947         mutex_unlock(&q->vb_lock);
948 }
949
950
951 ssize_t videobuf_read_stream(struct videobuf_queue *q,
952                              char __user *data, size_t count, loff_t *ppos,
953                              int vbihack, int nonblocking)
954 {
955         int rc, retval;
956         unsigned long flags = 0;
957
958         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
959
960         dprintk(2, "%s\n", __func__);
961         mutex_lock(&q->vb_lock);
962         retval = -EBUSY;
963         if (q->streaming)
964                 goto done;
965         if (!q->reading) {
966                 retval = __videobuf_read_start(q);
967                 if (retval < 0)
968                         goto done;
969         }
970
971         retval = 0;
972         while (count > 0) {
973                 /* get / wait for data */
974                 if (NULL == q->read_buf) {
975                         q->read_buf = list_entry(q->stream.next,
976                                                  struct videobuf_buffer,
977                                                  stream);
978                         list_del(&q->read_buf->stream);
979                         q->read_off = 0;
980                 }
981                 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
982                 if (rc < 0) {
983                         if (0 == retval)
984                                 retval = rc;
985                         break;
986                 }
987
988                 if (q->read_buf->state == VIDEOBUF_DONE) {
989                         rc = CALL(q, copy_stream, q, data + retval, count,
990                                         retval, vbihack, nonblocking);
991                         if (rc < 0) {
992                                 retval = rc;
993                                 break;
994                         }
995                         retval      += rc;
996                         count       -= rc;
997                         q->read_off += rc;
998                 } else {
999                         /* some error */
1000                         q->read_off = q->read_buf->size;
1001                         if (0 == retval)
1002                                 retval = -EIO;
1003                 }
1004
1005                 /* requeue buffer when done with copying */
1006                 if (q->read_off == q->read_buf->size) {
1007                         list_add_tail(&q->read_buf->stream,
1008                                       &q->stream);
1009                         spin_lock_irqsave(q->irqlock, flags);
1010                         q->ops->buf_queue(q, q->read_buf);
1011                         spin_unlock_irqrestore(q->irqlock, flags);
1012                         q->read_buf = NULL;
1013                 }
1014                 if (retval < 0)
1015                         break;
1016         }
1017
1018  done:
1019         mutex_unlock(&q->vb_lock);
1020         return retval;
1021 }
1022
1023 unsigned int videobuf_poll_stream(struct file *file,
1024                                   struct videobuf_queue *q,
1025                                   poll_table *wait)
1026 {
1027         struct videobuf_buffer *buf = NULL;
1028         unsigned int rc = 0;
1029
1030         mutex_lock(&q->vb_lock);
1031         if (q->streaming) {
1032                 if (!list_empty(&q->stream))
1033                         buf = list_entry(q->stream.next,
1034                                          struct videobuf_buffer, stream);
1035         } else {
1036                 if (!q->reading)
1037                         __videobuf_read_start(q);
1038                 if (!q->reading) {
1039                         rc = POLLERR;
1040                 } else if (NULL == q->read_buf) {
1041                         q->read_buf = list_entry(q->stream.next,
1042                                                  struct videobuf_buffer,
1043                                                  stream);
1044                         list_del(&q->read_buf->stream);
1045                         q->read_off = 0;
1046                 }
1047                 buf = q->read_buf;
1048         }
1049         if (!buf)
1050                 rc = POLLERR;
1051
1052         if (0 == rc) {
1053                 poll_wait(file, &buf->done, wait);
1054                 if (buf->state == VIDEOBUF_DONE ||
1055                     buf->state == VIDEOBUF_ERROR)
1056                         rc = POLLIN|POLLRDNORM;
1057         }
1058         mutex_unlock(&q->vb_lock);
1059         return rc;
1060 }
1061
1062 int videobuf_mmap_mapper(struct videobuf_queue *q,
1063                          struct vm_area_struct *vma)
1064 {
1065         int retval;
1066
1067         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1068
1069         mutex_lock(&q->vb_lock);
1070         retval = CALL(q, mmap_mapper, q, vma);
1071         q->is_mmapped = 1;
1072         mutex_unlock(&q->vb_lock);
1073
1074         return retval;
1075 }
1076
1077 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1078 int videobuf_cgmbuf(struct videobuf_queue *q,
1079                     struct video_mbuf *mbuf, int count)
1080 {
1081         struct v4l2_requestbuffers req;
1082         int rc, i;
1083
1084         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1085
1086         memset(&req, 0, sizeof(req));
1087         req.type   = q->type;
1088         req.count  = count;
1089         req.memory = V4L2_MEMORY_MMAP;
1090         rc = videobuf_reqbufs(q, &req);
1091         if (rc < 0)
1092                 return rc;
1093
1094         mbuf->frames = req.count;
1095         mbuf->size   = 0;
1096         for (i = 0; i < mbuf->frames; i++) {
1097                 mbuf->offsets[i]  = q->bufs[i]->boff;
1098                 mbuf->size       += q->bufs[i]->bsize;
1099         }
1100
1101         return 0;
1102 }
1103 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1104 #endif
1105
1106 /* --------------------------------------------------------------------- */
1107
1108 EXPORT_SYMBOL_GPL(videobuf_waiton);
1109 EXPORT_SYMBOL_GPL(videobuf_iolock);
1110
1111 EXPORT_SYMBOL_GPL(videobuf_alloc);
1112
1113 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
1114 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1115 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1116
1117 EXPORT_SYMBOL_GPL(videobuf_next_field);
1118 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1119 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1120 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1121 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1122 EXPORT_SYMBOL_GPL(videobuf_streamon);
1123 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1124
1125 EXPORT_SYMBOL_GPL(videobuf_read_start);
1126 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1127 EXPORT_SYMBOL_GPL(videobuf_stop);
1128 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1129 EXPORT_SYMBOL_GPL(videobuf_read_one);
1130 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1131
1132 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
1133 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1134 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1135 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);