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